我有一组JSP页面,我想隐藏.jsp扩展名(经过一番研究,看来对SEO很有用)。
我遇到的一种解决方案是:
<servlet> <servlet-name>mypage</servlet-name> <jsp-file>/some-page.jsp</jsp-file> </servlet> <servlet-mapping> <servlet-name>mypage</servlet-name> <url-pattern>/some-page</url-pattern> </servlet-mapping>
虽然这样做可行,但我相信我必须为网站上的每个jsp页面设置此映射。
…,它使用一个简单的servlet来转发请求。在我的web.xml中,我可以执行以下操作:
<servlet> <servlet-name>MyServletName</servlet-name> <servlet-class>myservlets.PrettyUrlServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServletName</servlet-name> <url-pattern>/myservlet/*</url-pattern> </servlet-mapping>
现在的问题是我不想点击以下URL:www.mydomain.com/myservlet/some-page
我想使用以下网址:www.mydomain.com/some-page
所以我将url-pattern更改为“ / *”
<servlet> <servlet-name>MyServletName</servlet-name> <servlet-class>myservlets.PrettyUrlServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServletName</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
但这会导致无限循环:
Exception in thread "http-bio-8080-exec-1" java.lang.StackOverflowError at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:219) at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228) . . at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228) at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228) at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:379) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329) at myservlets.PrettyUrlServlet.doGet(PrettyUrlServlet.java:22)
我不确定该如何解决。有任何想法吗?
映射的servlet/*也将在RequestDispatcher#forward()调用上运行。因此,如果您在该servlet中执行转发,则每次都会在无限循环中调用自身。那解释了StackOverflowError。
/*
RequestDispatcher#forward()
StackOverflowError
毕竟,您根本不应该使用/*servlet。它仅在servlet过滤器上有意义。将Servlet映射放回更特定的URL模式,并创建一个过滤器,/*必要时将其转发到所需的servlet。您当然不希望Servlet处理例如images / CSS / JS文件。假设它们都放在/resources文件夹中,并且前端控制器已映射到/myservlet/*,则在中执行以下操作doFilter():
/resources
/myservlet/*
doFilter()
HttpServletRequest req = (HttpServletRequest) request; String path = req.getRequestURI().substring(req.getContextPath().length()); if (path.startsWith("/resources/")) { // Just let container's default servlet do its job. chain.doFilter(request, response); } else { // Delegate to your front controller. request.getRequestDispatcher("/myservlet" + path).forward(request, response); }