• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Map vs Static Resource

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A suffix strategy works well too:

/contextPath/somethingWonderful.do
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, except it makes people think you're using Struts... ewwwwwww.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you are advocating finely grained URL mappings?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you don't want to use either of the conventional prefix or suffix mapping?
 
Chris Corbyn
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic