File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Is there a way to know from which servlet a request has been redirected or dispatched ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is there a way to know from which servlet a request has been redirected or dispatched ?" Watch "Is there a way to know from which servlet a request has been redirected or dispatched ?" New topic
Author

Is there a way to know from which servlet a request has been redirected or dispatched ?

Baaziz Hamza
Greenhorn

Joined: Aug 30, 2012
Posts: 8

Hello there !
As i said in the title, if I have a controller servlet that accepts requests and redirections from multiple servlets (or any resource in general).
Is there any ways I could determine which jsp send that request or which servlet did that redirection/dispatching, without sending an extra param (origin="ServletName" for example) with every request ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

That's a red flag for a bad design.



Why do you need to know this? This creates strong coupling between the components, which can make your design fragile and easily broken.

If you need to make a decision based upon this info, rather than trying to find out where things came from, define a parameter that can be used to pass info that can be used to make the decision.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Baaziz Hamza
Greenhorn

Joined: Aug 30, 2012
Posts: 8

Bear Bibeault wrote:That's a red flag for a bad design.



Why do you need to know this? This creates strong coupling between the components, which can make your design fragile and easily broken.

If you need to make a decision based upon this info, rather than trying to find out where things came from, define a parameter that can be used to pass info that can be used to make the decision.


Thanks for the quick reply , I already know this solution, I'm just asking if there's a method or something in Servlet API, that does this automatically ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

So you want to create a bad design?
Baaziz Hamza
Greenhorn

Joined: Aug 30, 2012
Posts: 8

Bear Bibeault wrote:So you want to create a bad design?

No ! I guess sending an extra param holding the name of the servlet with every request, and making hidden inputs in the jsp forms will do it, i was just afraid this solution was somehow dumb and heavy
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Baaziz Hamza wrote:
Bear Bibeault wrote:So you want to create a bad design?

No ! I guess sending an extra param holding the name of the servlet with every request, and making hidden inputs in the jsp forms will do it, i was just afraid this solution was somehow dumb and heavy

You're instinct there is right -- sending the name of the Servlet is not a good approach.

Step back. Take a deep breath.

What is it that you really need to know? It's not the name of the sending the servlet. Abstract what you're trying to do in your mind and figure out what it is you really need to know. Where you "came from" is never going to be the right answer.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Baaziz Hamza wrote: making hidden inputs in the jsp forms will do it


Note, having importent values be hidden form fields from the jsp/html form is bad as well.

The first rule of security is to never trust the user's browser. You think it may be IE or Firefox or Chrome, but it could be a rogue program that is pretending to be a browser but is really trying to crack into your system.

Put the data into the session, and do not send it to and from the browser.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

Bear Bibeault wrote:
Baaziz Hamza wrote:
Bear Bibeault wrote:So you want to create a bad design?

No ! I guess sending an extra param holding the name of the servlet with every request, and making hidden inputs in the jsp forms will do it, i was just afraid this solution was somehow dumb and heavy

You're instinct there is right -- sending the name of the Servlet is not a good approach.

Step back. Take a deep breath.

What is it that you really need to know? It's not the name of the sending the servlet. Abstract what you're trying to do in your mind and figure out what it is you really need to know. Where you "came from" is never going to be the right answer.


One example is:
If called from form A i need to restrict the search on database to only include the current users records, if I am calling from form B i need to search for every user.

create a parameter called restrictSearchToCurrentUser (i like snappy variable names).

which means that if later on form B gets a check box saying "restrict to current user" it is a simple matter of passing the correct boolean to the method.

 
Don't get me started about those stupid light bulbs.
 
subject: Is there a way to know from which servlet a request has been redirected or dispatched ?
 
Similar Threads
Problem with RequestDispatcher and relative links
When the jsp contacts the servlet for the first time in a session
Redirecting to different pages using a servlet Controller
Original request referer in Action/Interceptor
Request parameter and Attribute??