Question

I have Apache Tomcat Version 7.0.26 running.

A have a JAX-RS servlet under /test, which calls jsps which in tourn should be handled by the Tomcat servlet engine.

E.g., /test should be handled by jersey, but /test/result.jsp should be handled by the JSP engine.

I don't want the user to see different urls, therefore, I rewrite the jsp URLs to /jsp internally.

Config is like that:

UrlRewriteFilter (in urlrewrite.xml):

<rule>
    <from>^/([^j][^s][^p].*\.jsp)</from>
    <to>/jsp/$1</to>
</rule>

web.xml:

<filter>
   <filter-name>UrlRewriteFilter</filter-name>
   <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
   <!-- set the amount of seconds the conf file will be checked for reload
   can be a valid integer (0 denotes check every time,
      empty/not set denotes no reload check) -->
   <init-param>
     <param-name>confReloadCheckInterval</param-name>
     <param-value>0</param-value>
   </init-param>
</filter>
<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

<servlet>
  <servlet-name>Test</servlet-name>
  <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
  <init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>test</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>Test</servlet-name>
  <url-pattern>/test/*</url-pattern>
</servlet-mapping>

A call to /xyz.jsp is redirected to /jsp/xyz.jsp. A call to /test/xyz.jsp leads to 404 - Servlet Test is not available. I thought that filters have precedence over servlets, don't they?

I am aware of the related question Servlet vs Filter, but they don't state anything about the precedence.

BTW: PrettyFaces seems to be similar to UrlRewriteFilter, but I haven't tried it yet. I (currently) like UrlRewriteFilter more.

EDIT: The question How to mix server-side Jax-rs call with native files without prefix? asks the question behind my question.

Was it helpful?

Solution

Actually you can make it work quite easily and you don't need any url rewriting. Just place your jsp's into test/ dir, register Jersey as a servlet filter instead of a servlet (i.e. replace all occurrences of servlet with filter in your web.xml) and add com.sun.jersey.config.feature.FilterForwardOn404 init param set to true. The resulting web.xml should look as follows:

<filter>
  <filter-name>Test</filter-name>
  <filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
  <init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>test</param-value>
  </init-param>
  <init-param>
    <param-name>com.sun.jersey.config.feature.FilterForwardOn404</param-name>
    <param-value>true</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>Test</filter-name>
  <url-pattern>/test/*</url-pattern>
</filter-mapping>

OTHER TIPS

It seems that such a setup does not work. Placing all rest services unter /rest and all jsps under /jsp does the trick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top