我有一个带有 url 映射""
(空字符串)的 servlet。它应该被映射到上下文根(我在 eclipse 中使用 tomcat7)。
但有时当我刷新浏览器时,页面显示 404 页面。一个快速的解决方法是重新运行(在 Eclipse 中)。过了一会儿,同样的 404 错误又回来了。我对此感到非常沮丧。有没有办法追踪映射的 url 模式有时(大部分时间都可以)如何导致 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。