my dog learned polymorphism*
The moose likes Servlets and the fly likes From where request is made? 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 "From where request is made?" Watch "From where request is made?" New topic
Author

From where request is made?

Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
Hi,

I am making a project, in which it is demand that there should be one servlet "navigationServlet" which should handle all the navigation of the web. I have to redirect the request to specific servlets, but sometimes i have to make decision depending on the situation from where the request has been made.

Is there any way to find it out from where the request has been made? right now i am using session to store the current page being displayed (i am not using query string for this purpose). Can you people tell me any good solution?


The difference between <b>failure</b> and <b>success</b> is often being <b>right</b> and being <b>exactly right</b>.
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
request.getRequestURI()


MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
well, i have checked that, but it is either giving me the NULL value or giving me the value of the servlet that has recieved request ...

my, signUp.html is requesting to navigationServlet, and when i print the request.getRequestURI() it prints the
http://127.0.0.1:8084/siteName/NavigationServlet ... !
[ August 21, 2006: Message edited by: Muhammad Ali Amin ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410



This will return null if someone types the URL in by hand or uses a bookmark.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

And browsers are not required to set it, so it is not reliable


[My Blog]
All roads lead to JavaRanch
Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
Originally posted by Ben Souther:


This will return null if someone types the URL in by hand or uses a bookmark.


so if it returns null, if we types the URL, then how can we use it at runtime to check from where the request is made.

??
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
request.getRemoteAddr() will give you the IP address from which the request was received.
There's a request header "user-agent" which will tell you (if it's set) what the client claims to be (which is not what it actually is, as many non-browser clients and some browsers fake that header get get past certain firewalls that block all http traffic not coming from IE or Netscape derived browsers).


42
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I think you need to clarify what you mean by "From where the request is made".

Are looking for the IP number of the client that made the request or are you looking for the page that contained the link or form that the user clicked to generate the request to your servlet?
Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
i am sorry for not making things clearer. WEll i want to know from which page the request is being generated.

i.e i am having a link say "A" on pageA and pageB ... if the link is clicked via pageA i have to goto some version say AXX and if the link is clicked from pageB i have to go some version say AYY.

I have set the link values in DB, so i can't put the link in HTML while generating it since for value A i am having link A in DB ... the value XX and YY are being genreated at runtime depending upon the page from it is being clicked.

I know i can handle it using the queryString by adding a form and a hidden field, but i want to know is there any build in mechanism in JAVA(request, response)?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

i want to know is there any build in mechanism in JAVA(request, response)

I don't think there is, except setting a parameter, or calling different controllers for each action.
Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
Setting the parameter --- is the approach right now i am using
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60749
    
  65

Originally posted by Muhammad Ali Amin:
Setting the parameter --- is the approach right now i am using


Why the sad face? It's the right way to do it. Trying to figure out where the page was invoked from in order to branch on processing was a really poor design choice. By doing so, you would have created a lot of implicit bindings and co-dependencies among your pages that would make them confusing and fragile.

Think about it from the point of view of Java methods. To cause a method to branch on processing, do you inspect the stack trace to figure out from where the method was called? Of course not. That would be ridiculous. Rather, you pass a parameter to the method that causes it to act in the manner that the caller desires.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

To bring back a smile, I'd like to add that Spring is actually using hidden parameters for its WizardForms. One indicating the current page, one indicating the target page.

(I am not saying that because Spring does it this way, then you should do it the same way )
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
It's not a bad system. In fact it's the logical solution.
The alternative would be to have a database of some sort to store every possible combination of origin-destination and have each page contain keys into that database for each button or link that goes to another page.

I've used both in the past, both work well.
Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226


thanks for your comments.

Well, actually it was first time that i am designing the application beside coding it. That's why i was thinking about the most flexible and good design. I don't know the design i have done is right or not. That's why i wanted to discuss what i have implemented.


P.S:

Can you people suggest me how can i evaluate my design? Should i post the real requirement with the design i opted for in some topic here?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: From where request is made?
 
Similar Threads
Faces Redirecting issue
response generation issue.
Configuration GUI & suncertify.properties
Flow of data from Init Servlet to jsp
Error messsages in Spring MVC.