这是我的Java类,我通过它调用简单的servlet并传递数据,我是 使用URL和HttpURlConnection类。什么应该是servlet的URL路径
public class TestJava
{
public static void main(String[] args)
{
try
{
URL url=new URL("http://localhost:9865/TestingServlet/PushServlet");
HttpURLConnection http_url =(HttpURLConnection)
url.openConnection();
http_url.setRequestMethod("POST");
http_url.setDoOutput(true);
http_url.setDoInput(true);
InputStream response = http_url.getInputStream();
System.out.println(" " +response);
ObjectOutputStream objOut = new
ObjectOutputStream(http_url.getOutputStream());
objOut.writeObject("hello");
objOut.flush();
objOut.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
这是servlet代码,我从java代码接收对象并显示
它在控制台上。
public class PushServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try
{
System.out.println("HELLO This is servlet");
ObjectInputStream objIn = new
ObjectInputStream(request.getInputStream());
TestJava p = null;
p = (TestJava) objIn.readObject();
System.out.println("Servlet received p: "+p);
}
catch (Throwable e)
{
e.printStackTrace(System.out);
}
}
我的web.xml就像这样
<welcome-file-list>
<welcome-file>
Customer_Servlet
</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>PushServlet</servlet-name>
<servlet-class>com.servlet.PushServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PushServlet</servlet-name>
<url-pattern>/PushServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>50</session-timeout>
</session-config>
</web-app>
当我试图在服务器上运行java代码,即Apache服务器时,我得到了 错误HTTP状态404我无法找到为什么我收到此服务器错误 我的代码都是关于从java应用程序调用servlet
please help me guys .
答案 0 :(得分:0)
所以回答你的问题:“什么应该是servlet的url路径?”我想你应该知道以下内容:
网址应为:
http://localhost:portNumber/nameOfTheContext/MappingOfTheServlet WHERE
portNumber = the port number of the server ( Apache has it by default to 80 but Apache Tomcat has it to 8080, so it's not 9865 unless you changed it from configuration file)
nameOfTheContext = the name of the folder/archive( with .WAR or .EAR extension) which you deployed to the server
MappingOfTheServlet = the mapping of the servlet which you put in web.xml. So if you have <url-pattern>/PushServlet</url-pattern> then the mapping is PushServlet.
所以说你的网址应该是:
http://localhost:8080/NameOfTheContext/PushServlet
如果不清楚在哪里可以找到NameOfTheContext告诉我们你是如何部署servlet的,那么我们可以帮助你。
稍后编辑:上下文的名称应为: JavaToServlet ,因为这是 .WAR 存档的名称。所以你的网址应该是:
http://localhost:9865/JavaToServlet/PushServlet
答案 1 :(得分:0)
来自网络上404 Error WiKi
的参考资料当用户尝试关注损坏或死链接时,网站托管服务器通常会生成“404 Not Found”网页;
您可以通过以下链接更多地了解404错误 404 Not Found Error (What It Is and How To Fix It)