开发者

Accessing a servlet's mapping from Tomcat?

开发者 https://www.devze.com 2022-12-31 23:55 出处:网络
The following code accesses a servlet\'s name: servletConfig.getServletName(). Can I access a servlet\'s URL pattern in a similar way?

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").

0

精彩评论

暂无评论...
验证码 换一张
取 消