通用Servlet到JSP的映射 [英] Generic Servlet to JSP Mapping
问题描述
我有一个包含许多JSP文件的Web应用程序,并且想要删除.jsp扩展名而不显示在URL中,而不必将每个servlet映射到一个相似的页面名称.为此,我想以通用方式将所有servlet重定向到JSP文件,例如将/Login映射到/Login.jsp.
我将所有servlet映射到一个过滤器,如下所示.这适用于重定向到* .jsp的情况,但最终结果是空白页面的URL仍包含.jsp扩展名.
I have a web app with many JSP files and want to remove the .jsp extensions from displaying in the URL without having to map each servlet to a similar page name. To do this I would like to redirect all servlets to a JSP file in a generic manner such as mapping /Login to /Login.jsp.
I mapped all servlets to a filter as below. This works with respect to redirections to *.jsp except the end result is a blank page with the URL still containing the .jsp extension.
<servlet>
<servlet-name>PageNameFilter</servlet-name>
<servlet-class>PageName</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PageNameFilter</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
public class PageName extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String uri = request.getRequestURI();
if (!uri.endsWith(".jsp"))
{
String newPage = uri + ".jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(newPage);
dispatcher.forward(request, response);
}
else
{
// Here when we have a full URL (ie: /Login.jsp)
// ??? WHAT TO DO HERE ???
}
}
}
推荐答案
我终于使用了tuckey的urlrewrite在没有servlet映射的情况下动态地完成了此操作.请参阅此处的SO帖子 urlrewrite示例
I finally did this dynamically without servlet mappings using tuckey's urlrewrite. See the SO post here urlrewrite example
这篇关于通用Servlet到JSP的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!