• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Serving static assets from a servlet

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi-
I'm putting together a little mini-framework for a project I'm doing. I'm using a front controller servlet to process all requests (<url-pattern>/*</url-pattern> ) . Is there an easy way to serve static assets (images, css, some html). Basically, I just want to be able to identify the request as a static asset, and say "just do what you normally would do". I want to avoid having to actually open the file on the filesystem and stream it to the response. If i use request dispatcher, it will endlessly loop right back into the front controller, correct?

Also- it is a requirement that the browser-visible urls for this application are at the top level folder under the domain and have no file extension. ie- www.myapp.com/page1 so I can't do something like: <url-pattern>/app/*</url-pattern> or <url-pattern>*.do</url-pattern>

Thanks

[ September 29, 2008: Message edited by: Dudley Dawson ]
[ September 29, 2008: Message edited by: Dudley Dawson ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mapping /* is going to cause a lot of headaches, including the static resource issue. Why is it a requirement? What's wrong with having a reasonable servlet path? (E.g. /something/*).
 
Dudley Dawson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
Mapping /* is going to cause a lot of headaches, including the static resource issue. Why is it a requirement? What's wrong with having a reasonable servlet path? (E.g. /something/*).



believe me, i said the same thing - marketing folks, ya know?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Push back. This seemingly simple requirement is going to add a lot of unnecessary goo and fragility to your application.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I want to avoid having to actually open the file on the filesystem and stream it to the response.



But thats what the default request handler does - one way or another it has to be done.

Essentially handling all /* means you are replacing the default handler - if this was my problem, and I was working with Tomcat, I would remove the default handler mapping and just forward all requests that did not require custom handling to it.

Bill
 
Dudley Dawson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by William Brogden:


But thats what the default request handler does - one way or another it has to be done.

Essentially handling all /* means you are replacing the default handler - if this was my problem, and I was working with Tomcat, I would remove the default handler mapping and just forward all requests that did not require custom handling to it.

Bill



If I understanding you correctly, I think that's what I want to do, I just don't know how. BTW - I am using tomcat

How can i say: /images/*, /css/*, /flash/*, etc, use the default handler, while everything else use my front controller?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to solve the exact same problem, pretty frustrating how limiting the servlet mapping is. Anyway, I tried

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/images/*</url-pattern>
</servlet-mapping>

which stops sending it to my dispatcher, but I'm still getting a 404 on any image I request.
 
Dudley Dawson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Darrin Holst:
I'm trying to solve the exact same problem, pretty frustrating how limiting the servlet mapping is. Anyway, I tried

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/images/*</url-pattern>
</servlet-mapping>

which stops sending it to my dispatcher, but I'm still getting a 404 on any image I request.



Well, you are getting the 404 becasue you have no servlet named "default" in your DD. I guess what we're both looking for is a way to invoke the default handler, either programmatically or declaratively.
[ September 30, 2008: Message edited by: Dudley Dawson ]
 
Dudley Dawson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this can be solved with a servlet filter.
pseudocode:


i'll kick the tires on this and reply...
 
Darrin Holst
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dudley Dawson:


Well, you are getting the 404 becasue you have no servlet named "default" in your DD. I guess what we're both looking for is a way to invoke the default handler, either programmatically or declaratively.

[ September 30, 2008: Message edited by: Dudley Dawson ]



The "default" servlet is defined in the web.xml provided by the container. I figured it would be the same as mapping /jsp/* to the "jsp" servlet which is also defined only in the containers web.xml (and yes, that one works)
 
Darrin Holst
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Darrin Holst:
I'm trying to solve the exact same problem, pretty frustrating how limiting the servlet mapping is. Anyway, I tried

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/images/*</url-pattern>
</servlet-mapping>

which stops sending it to my dispatcher, but I'm still getting a 404 on any image I request.



So I did some more playing around with this and noticed a debug parameter on the default server (this is Tomcat 5.5 BTW) so I turned that on.

Requesting http://localhost/context-root/images/foo.gif shows "Serving resource 'foo.gif'" in the log.

Weird...so I request http://localhost/context-root/images/images/foo.gif and I get the image. Appears to removing the url-pattern before serving the resource.

Darrin
 
Darrin Holst
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, I think I got something working. This is the relevant part of my web.xml...

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>YourController</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

and then some magic comes in with a url rewriting filter from http://tuckey.org/urlrewrite/ with a rewrite rule to prepend the static node to images, css, etc

<rule>
<note>prepend static to static assets</note>
<from>^(/(?:images|css|javascript)/.*)$</from>
<to last="false">/static$1</to>
</rule>

Or I suppose you could just move all of your static asset directories under an actual directory named static.
 
He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic