• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can a servlet know the caller?

 
Jeya Balaji
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sudd Ghosh
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inside the doGet() or doPost() method in the servlet, use request.getParameter("caller"). Hope this helps.
Thanks, Sudd
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
kapil Gupta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!