Search code examples
javaxmljspservletsweb.xml

How to change the URL pattern from /index.jsp to /test/index.jsp by configuring web.xml?


I have jsp file named index.jsp in root folder (webcontent), till now I simply used to access the page using the following URL:

http://localhost:8080/Sample/index.jsp

Where Sample is the project name.

Now I was asked to change the URL pattern to the following without changing the location of index.jsp file (i.e. still the index.jsp be under webcontent folder):

http://localhost:8080/Sample/test/index.jsp

How to achieve this by configuring the web.xml?


Solution

  • This can be achieved by the following way

      <servlet>
        <servlet-name>BeerAppServlet</servlet-name>
        <jsp-file>/index.jsp</jsp-file>
      </servlet>
      <servlet-mapping>
        <servlet-name>BeerAppServlet</servlet-name>
        <url-pattern>/test/index.jsp</url-pattern>
      </servlet-mapping>
    

    In <servlet> instead of mapping it to <servlet-class> map it to <jsp-file>