如何调试servlet 404

时间:2013-05-10 14:28:49

标签: java tomcat servlets

我有一个带有url映射""的servlet(空字符串)。它应该被映射到上下文根(我正在使用带有eclipse的tomcat7)。

但有时当我刷新浏览器时,页面会显示404页面。快速解决方法是重新运行(在eclipse中)。过了一会儿,同样的404错误又回来了。 我对此非常沮丧。有没有办法跟踪地图上的映射网址模式有时(大部分时间都可以)导致404?

编辑1: 好的,这是配置和代码的细节: 我在tomcat服务器前面有一个apache httpd。 请求使用mod_proxy转发

<IfModule mod_proxy_http.c>
ProxyPass        /myapp           http://127.0.0.1:8080/myapp
ProxyPassReverse /myapp           http://127.0.0.1:8080/myapp
</IfModule>

这是servlet :( doPost和doGet都调用processRequest)

 @WebServlet("")
public class RootServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("Root Servlet");
        try {
            this.handleHomePage(request, response);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void handleHomePage(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, NamingException {

        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute(Config.SESSION_AUTH_EMAIL) != null) {
            String email = (String) session.getAttribute(Config.SESSION_AUTH_EMAIL);
            request.getRequestDispatcher("/WEB-INF/jsp/main/home.jsp").forward(request, response);
        }
        else {
            request.getRequestDispatcher("/WEB-INF/jsp/main/index.jsp").forward(request, response);
        }
    }


    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }

}

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/jsp/error/404.jsp</location>
  </error-page>

  <error-page>
    <error-code>500</error-code>
    <location>/WEB-INF/jsp/error/500.jsp</location>
  </error-page>
  <servlet>
    <servlet-name>info-about</servlet-name>
    <jsp-file>/WEB-INF/jsp/info/about.jsp</jsp-file>
  </servlet>
  <servlet-mapping>
    <servlet-name>info-about</servlet-name>
    <url-pattern>/about</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>info-help</servlet-name>
    <jsp-file>/WEB-INF/jsp/info/help.jsp</jsp-file>
  </servlet>
  <servlet-mapping>
    <servlet-name>info-help</servlet-name>
    <url-pattern>/help</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>info-contact</servlet-name>
    <jsp-file>/WEB-INF/jsp/info/contact.jsp</jsp-file>
  </servlet>
  <servlet-mapping>
    <servlet-name>info-contact</servlet-name>
    <url-pattern>/contact</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>info-terms</servlet-name>
    <jsp-file>/WEB-INF/jsp/info/terms.jsp</jsp-file>
  </servlet>
  <servlet-mapping>
    <servlet-name>info-terms</servlet-name>
    <url-pattern>/terms</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>info-privacy</servlet-name>
    <jsp-file>/WEB-INF/jsp/info/privacy.jsp</jsp-file>
  </servlet>
  <servlet-mapping>
    <servlet-name>info-privacy</servlet-name>
    <url-pattern>/privacy</url-pattern>
  </servlet-mapping>
</web-app>

eclipse打印的日志没有显示任何内容。只是404。 我发现在编辑内容并保存更改时会出现问题。 Eclipse将自动重新加载上下文,主页返回404。

1 个答案:

答案 0 :(得分:0)

好的,这可能是tomcat 7.0.40的错误 https://issues.apache.org/bugzilla/show_bug.cgi?id=54955