I have a set of JSP pages and I want to hide the .jsp extension (after a bit of research it seems it's good for SEO).
One solution I came across was the following:
<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>
And while this works, I believe I have to set up this mapping for every jsp page on my site.
I came across another solution posted here (Easy friendly URL's): Hidden features of JSP/Servlet
... which uses a simple servlet to forward the request. In my web.xml I have the following and it works fine:
<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>
Now the problem is I don't want to hit the URL: www.mydomain.com/myservlet/some-page
I want to use the URL: www.mydomain.com/some-page
So I changed the url-pattern to "/*"
<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>
But this causes an infinite loop:
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)
Which I'm not sure how to fix. Any ideas?