English 中文(简体)
找到多个服务器映射情景中使用的匹配的 URL 模式
原标题:finding the matched url-pattern used in a multiple servlet-mapping scenario

假设我有一个 Java 服务器, 我想用它做两个(或更多)不同的 URL 模式:

  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/this/exact/path</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/that/prefix/path/*</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/yet/another/exact/path</url-pattern>
  </servlet-mapping>

我的服务会被召唤到任何:

/this/exact/path
/yet/another/exact/path
/that/prefix/path/param1
/that/prefix/path/param2.html

我想知道的是,我怎样才能从我的代码中知道请求被匹配的路径是什么? (即,如果请求是向 /myapp/yet/another/exact/path 提出的,我想获得字符串 /yet/another/exact/path ) 。

最好有人能告诉我该如何做。

我尝试了 string 路径 = req.get truetractorURI () 但它也返回 /myap 部分 。

最佳回答

HtpServlet request.get ServletPath () 返回URL 模式, 不包括 / / code> 和 HtpServlet request. getPathInfo () 返回与 / / code > (或 null 匹配的部分 。

问题回答

您应该使用:

     /**
     *
     * Returns any extra path information associated with
     * the URL the client sent when it made this request.
     * The extra path information follows the servlet path
     * but precedes the query string and will start with
     * a "/" character.
     *
     * <p>This method returns <code>null</code> if there
     * was no extra path information.
     *
     * <p>Same as the value of the CGI variable PATH_INFO.
     *
     *
     * @return      a <code>String</code>, decoded by the
     *          web container, specifying 
     *          extra path information that comes
     *          after the servlet path but before
     *          the query string in the request URL;
     *          or <code>null</code> if the URL does not have
     *          any extra path information
     *
     */
    public String getPathInfo();




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...