views:

59

answers:

1

The following code accesses a servlet's name: servletConfig.getServletName(). Can I access a servlet's URL pattern in a similar way?

An excerpt from web.xml:

<servlet-mapping>
  <servlet-name>This is the servlet's name</servlet-name>
  <url-pattern>/this-is-its-url-pattern/*</url-pattern>
</servlet-mapping>
+2  A: 

Nothing is available in the Servlet API. Either parse the web.xml yourself, or duplicate it as an <init-param> of the servlet wherein you'd like to access it.

<servlet>
    <servlet-name>servlet</servlet-name>
    <servlet-class>com.example.Servlet</servlet-class>
    <init-param>
        <param-name>url-pattern</param-name>
        <param-value>/servlet</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>servlet</servlet-name>
    <url-pattern>/servlet</url-pattern>
</servlet-mapping>

This way it's available by servletConfig.getInitParameter("url-pattern").

BalusC