aspose file tools*
The moose likes Servlets and the fly likes Servlet Map vs Static Resource Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Map vs Static Resource" Watch "Servlet Map vs Static Resource" New topic
Author

Servlet Map vs Static Resource

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I put a servlet-mapping entry in web.xml to route /MyApp/* to a "front controller" servlet. Is there a way I can still serve static files like /MyApp/mywonderful.css or htm or jpg without going through the servlet?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

Sounds like a lot of pain and aggravation. Why not do what I generally do and use a specific prefix to route commands to the front controller.

Something like:

/contextPath/command/doSomethingWonderful


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
A suffix strategy works well too:

/contextPath/somethingWonderful.do


Fun programming etcetera!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

Yeah, except it makes people think you're using Struts... ewwwwwww.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yes, one goal is to not reveal anything about the technology, folder structure, program design, etc. URLs look like /Repo/Asset/123 but my js and css references look like /jsp/repo/repo.css.
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
So you are advocating finely grained URL mappings?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

In Front Man (link below) I use a properties file to associate the path info (stuff after the /command prefix) with the class that implements the Command. This make the implementation completely opaque.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Right everything Repo maps to one servlet. For that /Repo/Asset/123 style, I parse the word Asset out and that maps to a command. The tail 123 is key information. The URLs are not human friendly (what does 123 mean?) but they are stable so users can link and bookmark them. Some they can even chop to work up a hierarchy. Note the plural Assets now: Assets/001/002/003 or Assets/001/002 or Assets/001

BTW: I'm required to use a controller servlet that originated in the previous century. It expects query string parameters that make for ugly URLs. I made a filter that fakes those up as attributes based on the nice URL. That trick made my day.
[ August 03, 2007: Message edited by: Stan James ]
Chris Corbyn
Ranch Hand

Joined: Jan 14, 2007
Posts: 114
Coming from a PHP background I've often wondered why I've never stumbled upon request routers in Java other than the basic servlet mappings. It's easy to determine controllers and actions based on a URI like *.do mapped to the front controller, but what about friendly URIs generally?

/forum/createPost/threadid/23

Could easily be represented by a pattern like:

/:module/:action/*

Where :module and :action become request parameters "module" and "action" (forum and createPost respectively here). The * maps /key1/value1/key2/value2 so above "threadid" is "23".

Then you could take it a bit further by adding a new mapping:

/forum/createPost/:threadid

Now any calls the the createPost action have "threadid" mapped in by default so the URL becomes

/forum/createPost/23

A nice XML file containing mappings either a defined prefix or a defined suffix in web.xml to ensure all requests go through the front controller and you can have full control over clean URLs. Without using something like mod_jk and mod_rewrite

Maybe something already does exist, or more likely it's not needed and I haven't realised it through my inexperience with Java
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

No one was saying anything about using mode_jk, mod_rewrite, or mod anything. Or even Apache for that matter.

What's with the colons in your URLs? Is that some php mechanism?

In any case, if I understand what you are saying, that's exactly the type of processing that we've been talking about by using the servlet path, and path info information provided by the request interface.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Again, that part works great. To swing full circle back to the beginning, I'd like

/Repo/some.html

to NOT go through my servlet but just pick up a static file.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

So you don't want to use either of the conventional prefix or suffix mapping?
Chris Corbyn
Ranch Hand

Joined: Jan 14, 2007
Posts: 114
Originally posted by Bear Bibeault:
No one was saying anything about using mode_jk, mod_rewrite, or mod anything. Or even Apache for that matter.

What's with the colons in your URLs? Is that some php mechanism?

In any case, if I understand what you are saying, that's exactly the type of processing that we've been talking about by using the servlet path, and path info information provided by the request interface.


No those URls with colons in are patterns represnting a URL. The colons are variables in the request so :module equates to request.getParameter("module").

Effectively:

?module=forum&action=createPost

Could have a mapping schema like:

/:module/:action

With a real URL looking like:

/forum/createPost

You'd write utils and view helpers to build URLs based on your list of mappings and you'd hydrate the request with values based on the rules in the mapping schema and parsing the URI. It's basically just exactly what you guys were talking about, except completely configurable and extendible. You can validate with patterns and provide defaults too.

Maybe this will offer some insight. symfony is a large PHP framework but this chapter simpl explains the configuration of the routing system.

http://www.symfony-project.com/book/trunk/09-Links-and-the-Routing-System

If I were to writ this in Java I'd have some XML file like this then a Router class and a collection of Route classes which help build URIs and hydrate the request.

 
Don't get me started about those stupid light bulbs.
 
subject: Servlet Map vs Static Resource
 
Similar Threads
path of a non-default app
workers2.props config
Servlet Mapping vs Static resource
Running servlet in Tomcat
Identify context path + servlet path+ path info for given URL