Using JSP with Jersey JAX-RS Implementation

This post shows you some tips you’ll likely need to use JSP with Jersey in typical Java webapps.

Tested Conditions

While Jersey 1.1.1-ea or later is probably the only hard requirement for the tips to work, my development environment is listed here for your info. You are welcome to add to this rather meager basis for sanity.

  1. Jersey 1.1.1-ea
  2. Tomcat 6.0.20
  3. JDK 1.5
  4. OS X Leopard

Change JSP Base Template Path

Default base path for templates is the root of the webapp. So if my webapp is at “/…/webapps/myapp” then Viewable(“/mypage”, null) will map to “/…/webapps/myapp/mypage.jsp”

To change this, say to “WEB-INF/jsp” as it’s commonly done for security reasons, add following init-param to Jersey servlet/filter in web.xml:


Return Viewable as part of Response

It was not obvious to me (doh) where Viewable fits into Response when I have to return a Response instead of Viewable. It turns out, Viewable can be passed where message body entity is passed. Example:

return Response.ok(new Viewable("/mypage", model).build();

Use “/*” as servlet-mapping for Jersey

The primitive servlet-mapping URI pattern scheme, which somehow survived many iterations of the servlet API, impacts JAX-RS hard if servlet-mapping is overly broad. Unfortunately, pretty restful URL calls for servlet-mapping to be “/*” instead of something like “/jersey/*”, breaking access to JSP files as well as static resources.

To work around, you’ll have to use Jersey as a filter instead of a servlet and edit a regular-expression init-param value to punch passthrough holes in Jersey’s routing scheme. To enable this, replace Jersey servlet entry in web.xml with something like this:


That’s all for now. Hope this post saved you some headaches.

This entry was posted in General, Technical and tagged , , , . Bookmark the permalink.

10 Responses to Using JSP with Jersey JAX-RS Implementation

  1. Casper Bang says:

    Thanks Don for the useful tip regarding getting around the limitations of url-pattern with Jersey!:)

  2. Thanks for this tip. Your statement “Hope this post saved you some headaches” was quite an understatement. I was already implementing other means of templating HTML results when I found out – from your post – that Jersey has this build-in.

  3. Pingback: HATEOAS by Example « Zienit

  4. I recently discovered that I need to use the filter configuration instead of servlet configuration of Jersey so that my JSF app will keep working. However, I did not define a WebPageContentRegex. Instead, I just mapped the filter to /rest/* and my web content continues to work.

  5. Canvas Art says:

    This is exactly the info. I have been searching for. Thank you for the informative post.

  6. Chris says:

    Thx, for bewaring me going crazy.
    after doing some refactoring in my webapp including the movement to the /* url-pattern for the jersey-Servlet, i nearly got crazy.

  7. Pingback: Revue de presse – semaine 22 - Espace de fouille

  8. Gianluca Orlando says:

    Sorry but this not working for me

    I got this exception

    javax.servlet.ServletException: non-HTTP request or response

    I’m working to solve this problem

  9. Art says:

    Great info, jsp finally works with jax for me.

  10. Tomek says:

    You saved my day. I’ve been looking for this for a couple of days now.

Comments are closed.