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>
In Servlet 3.0 (or Java EE 6) spec exist something:
http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRegistration.html
You can get a ServletRegistration using ServletContext.html#getServletRegistration.
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")
.
精彩评论