IntelliJ Java IDE
The moose likes JSP and the fly likes How can a servlet know the caller? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » JSP
Reply Bookmark "How can a servlet know the caller?" Watch "How can a servlet know the caller?" New topic
Author

How can a servlet know the caller?

Jeya Balaji
Ranch Hand

Joined: Jan 02, 2003
Posts: 40
Hi friends,
I have a jsp by the name sample.jsp
Using post or get method, control goes to dispatcher servlet.

Is there a method for the servlet to know the caller? ie to know sample.jsp or xyz.jsp - whoever passed the control.
At present I am doing something like :
<a href='dispatcher?caller=sample.jsp'>
for get
and
<input type='hidden' name='caller' value='sample.jsp'>
for post.

Is there a simple method (thru request object) to acheive this?
Regards,
Balaji


Regards,<br />Balaji
Sudd Ghosh
Ranch Hand

Joined: Oct 23, 2002
Posts: 187
Inside the doGet() or doPost() method in the servlet, use request.getParameter("caller"). Hope this helps.
Thanks, Sudd


Sun Certified Java2 Programmer-1.4<br />Sun Certified Web Component Developer for J2EE Platform<br />Sun Certified Business Component Developer for J2EE1.3
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I don't think that was what he was asking.
Have a look at the HTTP specification, it includes all of the pieces of information that is available on the HTTP header.
The one you will be interested is HttpReferrer, but you'll have to look for the exact spelling, then you can use request, getHeader (again not sure on the method call, my bandwidth is currently too low to go look )
Is this more what you wanted?
The 'gotchas' you'll run into is that the HTTP referrer must be supported by the HTTP client (ie browser) and is easy to spoof - so don't rely on the data.
Other methanisms you may try for tracking users is things like tokens. You attach a token to a page then see where that token gets passed to next. It is similar to your solution, but it is reusable and can be associated with the session. Tokens can be fooled by someone having multiple browser windows open with the same session id, but again there are ways around this if you are careful.
I haven't used Struts, but there may some other MVC style ways to track this too.
Dave


[ JavaRanch FAQ ][ Book Promotions ][ DbTamer ][ BumperStickers ][ JavaRanch Badges ]
kapil Gupta
Ranch Hand

Joined: Dec 17, 2001
Posts: 89
I think the better way is to pass a pageID when submitting each JSP page. For e.g in your jsp:
< form action="/ControllerServlet?pageID=1">
In the the servlet use getParameter to get the value of pageID :
if(request.getParameter("pageId") != null) {
pageId = request.getParameter("pageId");
}
Then use Switch/Case statement to perform the operation as required for a particular JSP.
Regards,
Kapil
Jeya Balaji
Ranch Hand

Joined: Jan 02, 2003
Posts: 40
Dave, thanks for the lead. I guess I will go by my method itself. ie to pass the caller ...
Kapil, the suggestion u gave will not be suitable in my case; because the form, link are generated in a semi-automated manner. The jsp page(s) belong to different person and I own the dispatcher code.
Thanks for your help,
Balaji
 
 
subject: How can a servlet know the caller?
 
Threads others viewed
prefix/suffix mapping
Blank page returned when filters are used with struts
requestDispatcher.forward() throws error when forwarded to #topic
Sample web.xml.. may be useful for SCWCD Ranchers
error-page for ViewExpiredException ignored by my jsf implementation
Two Laptop Bag