users@jersey.java.net

Re: [Jersey] Static references from JSP

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 02 Sep 2008 08:46:18 +0200

On Sep 2, 2008, at 8:13 AM, Craig McClanahan wrote:

> Paul Sandoz wrote:
>> Julio Faerman wrote:
>>> I have tried to write out the resource, but this is troublesome
>>> as the
>>> resource is not on the classpath. I think there is a method to
>>> get web
>>> resources in servlet 2.5,
>>
>> You need to use methods on the ServletContext.
>>
>>
>>> but i also think that the forward should
>>> work.
>>
>> I cannot get it to :-( it works for JSPs, but not for "static"
>> stuff like HTML and images, perhaps it is bug in the GF servlet
>> implementation i am using?
>>
>>
> What servlet mappings do you have defined?
>

"/" or "/*". I want to avoid a URL for the main application like:

  http://host:80/<deployment base>/<jersey base>

and exposing out the other resources (static or JSP pages) explicitly.

I can forward to JSPs but not to static content.

For example, if i have the following in the web directory:

  com/foo/Foo/index.jsp
  com/foo/Foo/image.png

And a resource class:

   package com.foo;
   @Path("/")
   public class Foo { ... }

Then the a path of "/" slash matches Foo and Jersey can forward the
servlet request to "com/foo/Foo/index.jsp" but any reference in the
JSP to "image.png" does not work.


> A typical servlet container will have a "default" mapping
> (essentially for pattern "/*") that is used to serve static
> resources. If you've defined your mapping to the ServletAdaptor or
> ServletContainer servlet to "/*", then you are overriding this
> default, meaning you'd need to take responsibility for serving the
> static resources yourself. This is technically feasible -- you
> could build static file service into ServletAdaptor or
> ServletContainer -- but a lot easier if you have the JAX-RS servlet
> mapped to something else.
>

Thanks. Yes, i want to avoid duplicating such effort. I also sent an
email to dev_at_glassfish.dev.java.net asking more details.

Paul.


> Craig
>
>>> The viewable meta-data seems a good feature, but i think it would
>>> need
>>> to support a transparent and implicit mechanism to access resources.
>>> Perhaps using reasonable "default" meta-data.
>>>
>>
>> Yes, now if only i could get access to the information Servlet
>> uses for static pages .e.g. it returns generated etags etc.
>> Although it does not seem to support 304 not modified correctly.
>>
>> Paul.
>>
>>
>>> On Mon, Sep 1, 2008 at 1:03 PM, Paul Sandoz <Paul.Sandoz_at_sun.com>
>>> wrote:
>>>> Hi,
>>>>
>>>> I have a fix, but i am not entirely happy with it.
>>>>
>>>> It appears that forwarding to a non-JSP resource does not work.
>>>> Even though
>>>> i can create the dispatcher and call forward. Somewhere
>>>> internally to the
>>>> Servlet implementation a 404 is returned. If anyone is a servlet
>>>> expert out
>>>> there perhaps they can shed some light on this.
>>>>
>>>>
>>>> The fix is for the JSP template processor to write out any
>>>> existing resource
>>>> other than a JSP file and dispatch/forward JSP files. Thus you
>>>> can add
>>>> images etc and implicitly they will get served as well was
>>>> explicitly
>>>> reference then in a Viewable, although the model makes no sense
>>>> in this
>>>> respect. But ideally i would like to forward to servlet and let
>>>> it write out
>>>> the resource with appropriate meta-data.
>>>>
>>>>
>>>> However, i think template processing is missing the ability in
>>>> general to
>>>> supply HTTP meta-data and for the viewable to be processed
>>>> taking into
>>>> account the template meta-data (e.g. precondition checking)
>>>> before the
>>>> template is written out. I am going to jot down some ideas here
>>>> so i don't
>>>> loose them:
>>>>
>>>> - If an entity in the ContainerResponse implements
>>>> ContainerResponseFilter then the filter method is executed
>>>> before the ContainerResponse is processed to write out the HTTP
>>>> meta-data and entity.
>>>>
>>>> - Viewable is modified to implement ContainerResponseFilter and the
>>>> filter method resolves the template and handles the meta-data.
>>>> Resolved state is retained on the Viewable instance for writing
>>>> out.
>>>>
>>>> - TemplateProcessor.resolve is modified to take as a parameter
>>>> request meta-data and return meta-data plus
>>>> resolved template path. Such meta-data can be:
>>>>
>>>> - content type
>>>> - content length
>>>> - expires
>>>> - last modified
>>>> - etag
>>>>
>>>> hence static data can be differentiated from dynamic data.
>>>>
>>>> Paul.
>>>>
>>>> Julio Faerman wrote:
>>>>> I am using a simple <img> tag. Also tried the URL directly,
>>>>> should be the
>>>>> same.
>>>>> I would suggest adding an static image to the bookstore sample,
>>>>> like a
>>>>> projetct logo.
>>>>> Returning a new Viewable for the image was my first attempt,
>>>>> but it
>>>>> results in an exception saying it could not find the template.
>>>>>
>>>>> The exception is:
>>>>> java.io.IOException: The template name, line-chart.gif, could
>>>>> not be
>>>>> resolved to the path of a template
>>>>>
>>>>> The code:
>>>>> @Path("/")
>>>>> public class RootResource {
>>>>> @GET
>>>>> public Viewable getJSP(){
>>>>> return new Viewable("index.jsp",""); //This works
>>>>> }
>>>>>
>>>>> @GET
>>>>> @Path("img")
>>>>> @Produces("image/gif")
>>>>> public Viewable getImage(){
>>>>> return new Viewable("line-chart.gif",""); //This
>>>>> does not.
>>>>> Files are
>>>>> in the same directory.
>>>>> }
>>>>> }
>>>>>
>>>>>
>>>>> Thank you for the attention and congrats all, this project is
>>>>> awesome.
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> On Mon, Sep 1, 2008 at 4:17 AM, Paul Sandoz
>>>>> <Paul.Sandoz_at_sun.com> wrote:
>>>>>> Hi Julio,
>>>>>>
>>>>>> Could you send me an example?
>>>>>>
>>>>>>> I am using implicit JSP Viewables (just like bookstore
>>>>>>> sample), but i
>>>>>>> don't know how can i reference a static resource (images,
>>>>>>> styles,
>>>>>>> scripts) from the page.
>>>>>>>
>>>>>> Does the following describe your problem?
>>>>>>
>>>>>> If i have a resource:
>>>>>>
>>>>>> com.foo.Foo
>>>>>>
>>>>>> and an index.jsp in:
>>>>>>
>>>>>> com/foo/Foo/index.jsp
>>>>>>
>>>>>> and there is an image at the location:
>>>>>>
>>>>>> com/foo/Foo/image.png
>>>>>>
>>>>>> how does "index.jsp" reference "image.png".
>>>>>>
>>>>>>
>>>>>>> I am trying a method that fowards to the requested resource, but
>>>>>>> servletContext.getRequestDispatcher().forward() is failling
>>>>>>> because
>>>>>>> the injected HttpServletRequest and Response are null. Is
>>>>>>> there a
>>>>>>> better way to do this?
>>>>>>>
>>>>>> If you are using Servlet you should be able to do:
>>>>>>
>>>>>> @Context HttpServletRequest req;
>>>>>>
>>>>>> @Context HttpServletRequest res;
>>>>>>
>>>>>> as fields or method parameters.
>>>>>>
>>>>>> You should be able to reference a static page using a
>>>>>> Viewable. Just
>>>>>> return
>>>>>> something like this:
>>>>>>
>>>>>> // Absolute reference
>>>>>> return new Viewable("/static.png", null);
>>>>>>
>>>>>> // Reference relative to the resource class or a super class
>>>>>> return new Viewable("static.png", null);
>>>>>>
>>>>>> Paul.
>>>>>>
>>>>>> -----------------------------------------------------------------
>>>>>> ----
>>>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>>>>
>>>>>>
>>>>> ------------------------------------------------------------------
>>>>> ---
>>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>>>
>>>> --
>>>> | ? + ? = To question
>>>> ----------------\
>>>> Paul Sandoz
>>>> x38109
>>>> +33-4-76188109
>>>>
>>>> -------------------------------------------------------------------
>>>> --
>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>>
>>>>
>>>
>>> --------------------------------------------------------------------
>>> -
>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>