Tomcat servlet - 状态404请求的资源不可用

时间:2015-12-10 13:37:21

标签: java tomcat servlets

在SO上有这样多个问题,但没有一个问题解决了我的问题,我无法想到可能导致此错误的任何事情。

我试图编写一个简单的servlet来打印" Hello World":

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class PrintServlet extends HttpServlet {


     public void doGet(HttpServletRequest request,
              HttpServletResponse response) throws ServletException, IOException {
         PrintWriter out = response.getWriter();
         out.println("Hello World");
     }
}

我所做的是编译java文件,将编译后的文件移动到/usr/local/apache-tomcat-7.0.65/webapps/examples/WEB-INF/classes,然后将这些文件添加到位于/usr/local/apache-tomcat-7.0.65/webapps/examples/WEB-INF的web.xml文件中:

<servlet>
  <serlet-name>PrintServlet</servlet-name>
  <servlet-class>PrintServlet</servlet-name>
</servlet>

<servlet-mappinng>
<servlet-name>PrintServlet</servlet-name>
<url-pattern>/servlet/PrintServlet</url-pattern>
</servlet-mapping>

并尝试使用http://localhost:8080/servlet/PrintServlet访问servlet。

我收到错误消息:状态404请求的资源不可用。

我不知道哪里可能出现错误,我只是按照教程进行操作。请有人帮帮我吗?

编辑:

catalina.out上的日志文件(通过调用tail -f catalina.out得到此信息,不知道如何打开此文件)。

Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /usr/local/apache-tomcat-7.0.65/webapps/examples has finished in 130 ms
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.65/webapps/host-manager
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /usr/local/apache-tomcat-7.0.65/webapps/host-manager has finished in 80 ms
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.65/webapps/manager
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /usr/local/apache-tomcat-7.0.65/webapps/manager has finished in 96 ms
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.65/webapps/ROOT
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /usr/local/apache-tomcat-7.0.65/webapps/ROOT has finished in 62 ms
Dec 10, 2015 4:46:02 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Dec 10, 2015 4:46:02 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 918 ms

调用./startup.sh时的启动消息:

Using CATALINA_BASE:   /usr/local/apache-tomcat-7.0.65
Using CATALINA_HOME:   /usr/local/apache-tomcat-7.0.65
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7.0.65/temp
Using JRE_HOME:        /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
Using CLASSPATH:       /usr/local/apache-tomcat-7.0.65/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.65/bin/tomcat-juli.jar
Tomcat started.

1 个答案:

答案 0 :(得分:2)

在你的web.xml中你说你有这个:

<servlet>
  <serlet-name>PrintServlet</servlet-name>
  <servlet-class>PrintServlet</servlet-name>
</servlet>

参见&lt; servlet-class&gt; PrintServlet &lt; / servlet-name&gt; 的区别?

应该是这样的:

<servlet>
  <serlet-name>test.PrintServlet</servlet-name>
  <servlet-class>PrintServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

纠正此问题,重启并报告是否有效; - )

如果那不起作用:

我想你让映射变得更加简单:

<servlet-mappinng>
  <servlet-name>PrintServlet</servlet-name>
  <url-pattern>/print</url-pattern>
</servlet-mapping>

哪个应解析为http://localhost:8080/examples/print

并向servlet添加一些调试输出:

package test;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class PrintServlet extends HttpServlet {

     static {
       System.out.println("STACKOVERFLOW:> I've been loaded!");
     }

     public void doGet(HttpServletRequest request,
              HttpServletResponse response) throws ServletException, IOException {
         PrintWriter out = response.getWriter();
         out.println("Hello World");
     }
}

重新编译,放入examples / WEB-INF / classes,重启tomcat并注意显示的行。或者日志显示错误消息?