tags:

views:

1106

answers:

4

Hi there. I have a Java web application which manages data for many clubs. I would like the application to show club information depending on the URL that is typed in.

Eg. If you enter "localhost:8080/MyApp/Club1" ...then the app should strip out the end of the requst url (Club1), do a lookup in the database for the club, and add this to the session. Then all database requests can look up data depending on which club is in the session.

If you enter "localhost:8080/MyApp/Club2", you get club to registered in the session, and all database requests are for club 2.

The way I thought I may do this is to use a servlet filter that calls HTTPServletRequest().getRequestURL(), and strips out the club name from the request url, to lookup the club from the database.

But then I wan't to change the request URL to be http://localhost%3A8080/MyApp so that the application works as normal. However, I don't think I am able to do this?

Anyone any ideas of how to do this, or if there is a better approach?

+2  A: 

I don't think a filter is the proper way to do it. What you're describing about changing behavior based on club membership is logic that belongs inside your service layer, where it can be reused.

A better way to do it would be to forget about filters and just program your servlet. Pass the club name as a parameter, use it to do a lookup in a map of the URL for that club, and have your servlet use the RequestDispatcher to redirect to the URL for that club. I don't see any advantage to intercepting it a heartbeat earlier with a Filter.

If you were using Spring, I'd recommend putting this into your service and let the web MVC tier handle mapping and routing of requests.

This is one of those cases where if it's too difficult and convoluted to do what you want, you're probably trying the wrong thing. There are easier ways to do this, and filters aren't even close.

duffymo
A: 

Core J2EE Patterns - Intercepting Filter

SUMMARY:Create pluggable filters to process common services in a standard manner without requiring changes to core request processing code. The filters intercept incoming requests and outgoing responses, allowing preprocessing and post-processing. We are able to add and remove these filters unobtrusively, without requiring changes to our existing code.

adatapost
Thanks for this. Any ideas on how best to do it?
Ofcourse, but those filters are doing things like logging, adding or altering parameters, and not redirecting to different URLs. I couldn't find an example of a filter that did such a thing.
duffymo
Thanks. It sounds like I'm off the mark using filters so should abandon that approach. I'm still not sure what the best way to do this is however. I think the problem boils down to this really.I know I can easly load club data if the user types in:localhost/myApp?club=myClubThat's just a case of getting the request parameter as soon as the request comes in, then getting the club from the database.I just don't seem to have found a way to do exactly the same thing, but get the club name from the path, rather than as a request parameter.
I'm probably missing something really obvious, but the fact that the URL contains the club name, is causing problems.Just thought there must have been a lot of people that have done this already.
As per your problem definition - I am agree with @duffymo. However it is possible to intercept request using filter and HttpServletRequestWrapper class. Have a look at this article - http://www.developer.com/java/ent/article.php/10933_3619786_1?comment=15405-0
adatapost
A: 

In your servlet's do_GET/do_POST, you can interpret the first part of the URI as a club, add it to the session (using application scope) if it's not already there, then delegate to other code which only looks at the rest of the URI and uses the in-session club where appropriate.

Vinay Sajip
+1  A: 

Take a look at urlrewritefilter. It's a servlet filter that can modify the request URL before your servlet sees it. It is often used to transform query parameters to or from path parameters.

If you google for it, you'll see it come up in the stack-traces of some pretty high-profile java applications :)

Adrian Pronk