This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have an un-usual requirement: I need to change the 'url mapping' of some servlets/JSPs, *but* this must be done dynamically.
i.e. : suppose my end-user opens up a browser and asks to see 'http://myHost/myWebApp/hello' Then on some condition I need the request to be handled by ServletA, on other conditions - ServletB. (Please don't ask why - it's a painful story, involving security, ancient content-management tools, and stupid management).
Theoretically, I could have a single servlet with some 'if' condition, but that's maintenance nightmare. I'd rather have 2 separate Servlets (ServletA, ServletB) and some external point of decision that would direct the requests to either one of them.
Could anyone please recommend the best way to do it?
1. Is there some built-in support for this, in the servlet/JSP specification ? I would even settle for vendor-specific solutions (preferrably, Tomcat or Websphere).
2. Could I achive this with a Filter (that is, javax.servlet.Filter) ? If so, could anyone please tell which syntax/method to use ?
You can't change the mapping, but you could use a filter to forward the request to either servlet based upon whatever decision you might make in the filter.
I do this all the time for authenticated pages. A filter determines if the user is logged in or not (based upon whether an authentication token has been placed on the session) and forwards them to the requested resource if so, or forwards them to the "show me the login page" controller if not.