title: Jersey Guice JSP how to?
body: <p>I'm using tomcat 6, jersey 1.8 with jersey guice and guice 3. 
  I'm having problem using JSP with my setup. 
  I have a "Status" servlet served as a simple servlet configured by web.xml, a jersey servlet configured by GuiceFilter which returns a jsp view response (jsp is /diff/index.jsp) to render the result as in: </p>
  
  <pre><code>Viewable view = new Viewable("/diff/index.jsp", null); 
  Response response = Response.ok().entity(view).build(); 
  return response;
  </code></pre>
  
  <p>It all works perfectly with simple Jersey, once I'm trying to have it with Guice integration the JSP fails and I'm getting a 404 response with "The requested resource (/diff/index.jsp) is not available." </p>
  
  <p>Using the debugger I can see that the JSPTemplateProcessor as called and got a RequestDispatcher with a StandardWrapper that has "isJspServlet = true" and "jspFile = null". </p>
  
  <h1>The web.xml looks like this:</h1>
  
  <pre class="lang-xml prettyprint-override"><code>  &lt;servlet&gt;
          &lt;display-name&gt;Status Page&lt;/display-name&gt;
          &lt;servlet-name&gt;Status&lt;/servlet-name&gt;
          &lt;servlet-class&gt;my.BaseStatusPage&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Status&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/Status/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
     &lt;filter&gt;
          &lt;filter-name&gt;guiceFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;com.google.inject.servlet.GuiceFilter&lt;/filter-class&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;guiceFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/REST/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;my.GuiceServletConfig&lt;/listener-class&gt;
      &lt;/listener&gt;
  </code></pre>
  
  <p>===================== </p>
  
  <p>GuiceServletConfig: </p>
  
  <pre><code>public class GuiceServletConfig extends GuiceServletContextListener { 
  
    @Override 
    protected Injector getInjector() { 
      return Guice.createInjector(new JerseyServletModule() { 
  
        @Override 
        protected void configureServlets() { 
          bind(DiffPage.class);// the jersey servlet 
  
          Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;(); 
          params.put(PROPERTY_PACKAGES, "my"); 
          params.put(PROPERTY_WEB_PAGE_CONTENT_REGEX, ".*\\.jsp"); 
          params.put(FEATURE_REDIRECT, "true"); 
          params.put(FEATURE_IMPLICIT_VIEWABLES, "true"); 
          params.put(RESOURCE_CONFIG_CLASS, "com.sun.jersey.api.core.PackagesResourceConfig"); 
  
          serve("/REST/*").with(GuiceContainer.class, params); 
        } 
      }); 
    } 
  </code></pre>
  
  <p>===================== </p>
  
  <p>Having GuiceContainer as a filter made the servlets served from the web.xml fail. Adding a jsp servlet in web.xml didn't do much good. </p>
  
  <p>Btw, I've read the thread from Jul 25, 2010 at <a href="http://jersey.576304.n2.nabble.com/Jersey-Guice-JSP-how-to-td5349718.html" rel="nofollow">the jersey mailing list</a> but it didn't work for me. </p>
  
  <p>Help appreciated 
  Thanks, Eishay </p>
  
  <p>-- Appendix --
  I find myself calling JSP from the business logic code. Ugly but works:</p>
  
  <pre><code>  protected Response renderJsp(HttpServletRequest request,
        HttpServletResponse response, ServletConfig servletConfig) {
      request.setAttribute("org.apache.catalina.jsp_file", "/diff/index.jsp");
      Class jspServletClazz;
      try {
        jspServletClazz = forName("org.apache.jasper.servlet.JspServlet");
        Object jspServlet = jspServletClazz.getConstructor().newInstance();
        jspServletClazz.getMethod("init", ServletConfig.class).invoke(jspServlet,
            servletConfig);
        jspServletClazz.getMethod("service", HttpServletRequest.class,
            HttpServletResponse.class).invoke(jspServlet, request, response);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
      return Response.ok().build();
    }
  </code></pre>