[英]Getting Server Error while calling Servlet from Java Application
这是我的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>
当我试图在服务器即Apache服务器上运行Java代码时,我正在
错误HTTP状态404我无法找到为什么得到此服务器错误我的代码全部是关于从Java应用程序调用servlet的
please help me guys .
因此,回答您的问题:“该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
从网上的404错误WiKi参考
当用户尝试访问断开或无效的链接时,网站托管服务器通常会生成“ 404 Not Found”网页。
您可以从下面的链接中进一步了解404错误
404 Not Found错误(这是什么以及如何解决) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.