This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes Serving static assets from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Serving static assets from a servlet" Watch "Serving static assets from a servlet" New topic
Author

Serving static assets from a servlet

Dudley Dawson
Ranch Hand

Joined: Dec 14, 2004
Posts: 57
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 ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61456
    
  67

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/*).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dudley Dawson
Ranch Hand

Joined: Dec 14, 2004
Posts: 57
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61456
    
  67

Push back. This seemingly simple requirement is going to add a lot of unnecessary goo and fragility to your application.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
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

Joined: Dec 14, 2004
Posts: 57
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?
Darrin Holst
Greenhorn

Joined: Sep 30, 2008
Posts: 4
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

Joined: Dec 14, 2004
Posts: 57
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

Joined: Dec 14, 2004
Posts: 57
I think this can be solved with a servlet filter.
pseudocode:


i'll kick the tires on this and reply...
Darrin Holst
Greenhorn

Joined: Sep 30, 2008
Posts: 4
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

Joined: Sep 30, 2008
Posts: 4
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

Joined: Sep 30, 2008
Posts: 4
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serving static assets from a servlet