2011-12-15 157 views
2

我有两个servlet(MainServlet和LoginServlet)和MainServlet处理所有请求并将其映射到/*。 LoginServlet进程请求并将其映射到/ login。我有一个html文件/html/login.html。现在我想在我点击http://localhost:8080/app/login时显示这个html文件。servlet url映射

LoginServlet doGet方法,我做httpServletRequest.getRequestDispatcher("login/login.html").include(httpServletRequest, httpServletResponse);

但这种重定向请求MainServlet。我无法将MainSerlvet的网址映射从/*更改为其他内容。

任何想法我可以如何实现以上? PS:如果问题不明确,请告诉我。

+0

您表示您的html文件位于“/html/login.html”,但看起来您正在为“login/login.html”获取请求调度程序。这是你的问题中的拼写错误吗?否则,这可能至少代表部分问题。 – shelley 2011-12-15 23:01:03

回答

1

您已将MainServlet映射到全局网址模式/*,这是一种很不好的servlet实践(这也包括像CSS/JS/images/etc这样的静态资源!)。这也将拦截所有转发和包含的请求。您需要将MainServlet映射到更具体的网址格式,例如/main/*,/app/*或类似的东西,并创建一个Filter映射到/*并转发所有非/login请求到MainServlet

String uri = ((HttpServletRequest) request).getRequestURI(); 

if (uri.startsWith("/login/")) { 
    // Just continue to login servlet. 
    chain.doFilter(request, response); 
} else { 
    // Forward to main servlet. 
    request.getRequestDispatcher("/main" + uri).forward(request, response); 
} 

顺便说一句,使用RequestDispatcher#include()来显示视图也不完全正确。相反,您应该使用RequestDispatcher#forward()