users@jersey.java.net

Re: [Jersey] RFH Jersey MVC - Reading JSPs from a Jar

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 25 May 2010 17:17:37 +0200

On May 25, 2010, at 5:03 PM, Imran M Yousuf wrote:

> On Tue, May 25, 2010 at 8:49 PM, Paul Sandoz <Paul.Sandoz_at_sun.com>
> wrote:
>>> <snip />
>>>
>>> the same manner as the resources. Can you please suggest me how to
>>> achieve it?
>>
>> You can change the base location where JSPs are located:
>>
>> https://jersey.dev.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/spi/container/servlet/ServletContainer.html
>> #JSP_TEMPLATES_BASE_PATH
>>
>> But i do not think that suits your need.
>
> From the looks of it, it does not indeed.
>
>>
>> How are you assembling the war? do you want JSPs to be served from
>> the
>> WEB-INF/classes or from jar files in WEB-INF/lib?
>>
>
> Actually since I was mainly thinking of maven dependency to a WAR
> packaged module, it would be from WEB-INF/lib.
>
>> AFAIK JSPs must be placed in the web area of the war.
>>
>
> Well, it very well could be since JSP in other cases are directly
> linked in the URI, but in case of Jersey that is not the case as its
> referred to using Viewable. So I guessed its a possibility; e.g.
> Apache Wicket does it.
>

Do you know if Apache Wicket uses JSPs as specified by Web
applications or uses its own technology that is compatible with JSPs?


> Any suggestions how I could achieve my desired behavior using Jersey?
>

The Jersey JSPTemplateProcessor looks up the JSP using the
ServletContext:

     public String resolve(String path) {
         if (servletContext == null)
             return null;

         if (basePath != "")
             path = basePath + path;

         try {
             if (servletContext.getResource(path) != null) {
                 return path;
             }

             if (!path.endsWith(".jsp")) {
                 path = path + ".jsp";
                 if (servletContext.getResource(path) != null) {
                     return path;
                 }
             }
         } catch (MalformedURLException ex) {
             // TODO log
         }

         return null;
     }

Then forwards using a request dispatcher:

     public void writeTo(String resolvedPath, Viewable viewable,
OutputStream out) throws IOException {
         // Commit the status and headers to the HttpServletResponse
         out.flush();

         RequestDispatcher d =
servletContext.getRequestDispatcher(resolvedPath);
         if (d == null) {
             throw new ContainerException("No request dispatcher for:
" + resolvedPath);
         }

         d = new RequestDispatcherWrapper(d, basePath, hc, viewable);

         try {
             d.forward(requestInvoker.get(), responseInvoker.get());
         } catch (Exception e) {
             throw new ContainerException(e);
         }
     }

JSPs are not directly processed and are required to be located as
specified for Web containers.

What i think you want to do may be supported by some JSP engines as an
additional feature beyond that required by Web containers but you
would need to write your own ViewProcessor to integrate with such
functionality.

Paul.