1
votes

I have a servlet with url mapping ""(empty string). It's supposed to be mapped to the context root (I'm using tomcat7 with eclipse).

But sometimes when I refresh the browser, the page displays 404 page. And a quick fix is to re-run(inside eclipse). And after a while the same 404 error comes back. I'm very frustrated about this. Is there a way to trace how on earth a mapped url pattern can sometimes (most of the time is OK) lead to 404?

EDIT 1: OK, here is the details of configuration and code: I have a apache httpd in front of the tomcat server. The request is forwarded using 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>

And here is the servlet: (processRequest is called by both doPost and doGet)

 @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>

The log printed by eclipse shows nothing. Just the 404. I found the problem will occur when I edit something and save the changes. Eclipse will auto reload the context and home page returns 404.

1
What do you mean re-run? When does the 404 occur? Is this the only URL impacted? - Brandon
re-deploy, there is no obvious reason when it will occur, and yes only root url is impacted. I try to print something inside the root servlet and it's not executed. So it's never called. - Yang
To map to context root use / url-mapping. - Sotirios Delimanolis
Can you provide some related code and configuration? - Sazzadur Rahaman
Maybe the servlet forwards to some other resource in case a specific condition is met, and this other resource doesn't exist. Check the logs, post the code. - JB Nizet

1 Answers

0
votes