Search code examples
jspservletsservlet-mapping

Generic Servlet to JSP Mapping


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 ???
        }
    }
}

Solution

  • I finally did this dynamically without servlet mappings using tuckey's urlrewrite. See the SO post here urlrewrite example