Search code examples
servletstomcat7

Tomcat URL Servlet Mapping


I have a sevelet with mapping like

<servlet-mapping>
      <servlet-name>Inventory</servlet-name>
      <url-pattern>/inventory</url-pattern>
</servlet-mapping>

I would like to create a url mapping to /invlist that goes to /inventory?q=list


Solution

  • <servlet-mapping> only maps a Servlet to a URL but cannot map a URL to URL.

    You need to create another Servlet for /invlist and in this Servlet , redirect to /inventory?q=list using HttpServletResponse#sendRedirect

    <servlet-mapping>
          <servlet-name>Inventory</servlet-name>
          <url-pattern>/inventory</url-pattern>
    </servlet-mapping>
    
    <servlet-mapping>
          <servlet-name>InventoryList</servlet-name>
          <url-pattern>/invlist</url-pattern>
    </servlet-mapping>
    

    Then in the InventoryList Servlet :

    httpServletResponse.sendRedirect("inventory?q=list")