从Java应用程序调用Servlet时获取服务器错误

从Java应用程序调用Servlet时获取服务器错误,java,servlets,Java,Servlets,这是我的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"); Htt

这是我的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>

所以要回答您的问题:“servlet的url路径应该是什么?”我认为您应该知道以下几点:

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.
如果不清楚在哪里可以找到上下文的名称,请告诉我们您是如何部署servlet的,以便我们可以帮助您

稍后编辑:上下文的名称应为:JavaToServlet,因为这是.WAR存档的名称。因此,您的URL应该是:
http://localhost:9865/JavaToServlet/PushServlet

来自web上的参考

当用户试图跟踪断开或死链接时,网站托管服务器通常会生成“404未找到”网页

您可以从下面的链接了解有关404错误的更多信息

.

这个Servlet是否映射到
PushServlet
?@Lutz-Horn:我已经在httpurlconnection的参数中给出了Servlet的名称。为什么您希望它可以在URL
http://localhost:9865/TestingServlet/PushServlet
?@Lutz-Horn:我不知道如何将它映射到servlet。在web.xml中,我已经在urlpattern中给出了servlet的url。发布您的web.xml代码,用于
PushServlet
声明。@Daniel:端口号我已从配置文件中更改,一切正常,但我无法澄清什么是“上下文名称”。我的java代码和servlet代码在同一个包中,我的项目是动态web项目。我只在apache服务器上运行java代码。@LetsCode您使用的服务器到底是什么:apache Http服务器还是apache Tomcat服务器?我猜您是从IDE(Eclipse、NetBeans、IntelliJ等)运行(启动/关闭)服务器的@Daniel:我在使用ApacheTomcat服务器,是的,我正在从Eclipse启动和停止服务器IDE@LetsCode所以我猜您使用的是ApacheTomcat7.x版本。如果这是正确的,那么应该有一个文件夹位于:
$CATALINA_HOME/webapps
,其中$CATALINA_HOME是安装的Apache Tomcat的位置。因此(在webapps文件夹中)应该部署您的文件夹/归档文件。文件夹的名称代表上下文的名称。@Daniel:谢谢,我遵循了与你提到的相同的过程,但仍然得到相同的错误,“Http 404状态”我在Apache Tomacat 6.0.32上运行。@Yagnesh:我已经通过了这个404错误wiki,但是我可以很容易地启动和停止服务器,但无法在执行servlet时调用servlet。如果应用程序有任何错误,或者应用程序部署正确,请在启动服务器时查看服务器日志。
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