This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamically changing url mapping for a servlet/jsp? Can I use filters?  RSS feed

 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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 ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66187
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot !
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!