2

I have a web application where I would like to tie a JSP to address http://host:port/status and a servlet to addresses like http://host:port/status/.... Is this possible? According to this article it should be possible ("container prefers an exact path match over a wildcard path match") at least for some containers and the Java Servlet Specification contains similar examples (albeit without wildcard, on p. 12-123 in April 2013 version), but if I try the following in web.xml it appears as if the JSP is never called and all requests (also to http://host:port/status) are routed to the servlet. My JSP and servlet are hosted on Google App Engine.

<servlet>
    <servlet-name>Status</servlet-name>
    <jsp-file>/Status.jsp</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>Status</servlet-name>
    <url-pattern>/status</url-pattern>
</servlet-mapping>
<servlet>
    <servlet-name>StatusUpload</servlet-name>
    <servlet-class>com.example.StatusUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>StatusUpload</servlet-name>
    <url-pattern>/status/*</url-pattern>
</servlet-mapping>
Community
  • 1
  • 1
Drux
  • 11,992
  • 13
  • 66
  • 116
  • What would even be the point of doing this? – developerwjk May 08 '14 at 21:53
  • @developerwjk E.g. this: data is collected via POST requests that arrive at `status/postfix`. Users can insect collected data at `status`. Keeps URL namespace clean IMHO. – Drux May 09 '14 at 08:45

1 Answers1

1

Instead of mapping the same url to two different JSPs/Servlets in web.xml you can use a URL Rewriting filter like Tuckey UrlRewriteFilter which uses a configuration file urlrewrite.xml which would also be placed in WEB-INF. It uses regex in the rules.

These two rules should do what you want:

      <rule>
        <from>^/status$</from>
        <to>/Status.jsp</to>
      </rule>
      <rule>
        <from>^/status/(.*)$</from>
        <to>/StatusUpload/?param=$1</to>
      </rule>

Then in WEB-INF you would not map the JSP anymore but would map the Servlet to StatusUpload. When the user goes to /status/postfix the URL Rewriting filter will forward to the servlet (with the postfix part passed as a parameter) in the backend without the address the user sees in the address bar changing.

developerwjk
  • 8,619
  • 2
  • 17
  • 33
  • +1 Interesting but too complex for my purpose (too many wheels). I'm still hoping this can be done on the level of `web.xml`/`url-pattern` alone. – Drux May 10 '14 at 06:30
  • It does require one more 174kb jar file. But since the rewriting rules use regex its much more powerful, and you won't have to parse the postfix part of your url into parameters yourself, which you will with the web.xml method. The reason I hesitated to suggest this at first was that it wasn't clear from the question that you are going to use path parts of the url as parameters, but I'm thinking you will. – developerwjk May 12 '14 at 17:03