• Post Reply Bookmark Topic Watch Topic
  • New Topic

Remember the page which posted

 
Erik Pragt
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
Is it possible in a servlet to see from which page the post was done, without using the http header 'referer' or using an attribute in the page.
For example, when I have a page (set_contents.jsp) which posts to a servlet (MyServlet), is it possible to see in the MyServlet to see that the post came from set_contents.jsp?
(the idea is to process the contents, and than to return to the original page without using a parameter such as 'origional page="set_contents.jsp")
Thanks for the help,
Erik Pragt
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could set a cookie with the name of the page in every page of your application.
Bill
 
David Hibbs
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Brogden:
You could set a cookie with the name of the page in every page of your application.
Bill

Wow is that overkill... you don't need to know this from every page, just for forms (or at least that's what's implied). Further, cookies are frequently blocked by users' browser preferences.
Simply put a hidden field in your form. You could even write a custom tag if you wanted that would generate a hidden field containing the page URL based on the request properties.
 
Erik Pragt
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William: now this would be an overkill! I said I didn't want to add an extra parameter, so I sure don't want to add a cookie, even if my client (IE, Opera, Nokia 6310i WML browser) has cookies turned on.
David: I don't want to add the parameter, so your hidden parameter is a good idea (and maybe the only one..... ) but this is the thing I actually did NOT wanted....so if there are any other solutions: please let me know!
Thanks, Erik
 
Sudd Ghosh
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be this message in the Servlet forum will help:
http://www.coderanch.com/t/281924/JSP/java/servlet-know-caller
Thanks, Sudd
 
David Hibbs
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Erik Pragt:
David: I don't want to add the parameter, so your hidden parameter is a good idea (and maybe the only one..... ) but this is the thing I actually did NOT wanted....so if there are any other solutions: please let me know!

Sorry, somehow skipped reading that in the original message... anyway, if you eliminate using request properties, and eliminate using request parameters, that leaves only request attributes or session attributes. Request attributes can't be set until you have a request, i.e. the form is submitted, so that's not an option. That leaves session attributes, which you can set when the form is displayed.
However, this opens the risk of mixing form data and form pages if the user does this:
1) Fill/Submit form 1
2) Fill/Submit form 2
3) hits back a few times to form 1's result page
4) hits refresh and opts to resubmit the form data
Besides which, this is not a good use of session data.
So unfortunately, you probably want to choose one of the options you wanted to avoid--using request headers or form parameters...
--David
 
Jignesh Malavia
Author
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the only thing that your servlet does is process the form data irrespective of the source JSP page and does not maintain any state information like a controller/dispatcher does, then you may want to convert it into a Filter and let it intercept the requests to all such JSP pages. Follow these steps:
1. Convert the Servlet (MyServlet) into a Filter (say, MyFilter).
2. Instead of posting to MyServlet, let the JSP page post to itself. <form action='/set_contents.jsp'>
3. In web.xml, map the URL /set_contents.jsp to MyFilter.
Originally posted by Erik Pragt:Is it possible in a servlet to see from which page the post was done, without using the http header 'referer' or using an attribute in the page. For example, when I have a page (set_contents.jsp) which posts to a servlet (MyServlet), is it possible to see in the MyServlet to see that the post came from set_contents.jsp?

Since each JSP page will use its own URI, the filter can distinguish between the different pages using getRequestURI() if it needs to. No need to use a separate paramter.
Originally posted by Erik Pragtthe idea is to process the contents, and than to return to the original page without using a parameter such as 'origional page="set_contents.jsp")
Add as many JSP pages as you want; just map them all to the same filter. You can use the wild character * too in the <url-mapping>. The FilterChain will take care of where to forward the request after the filter processes the contents.
If your servlet cannot be converted into a filter for any reason, then you will most probably have to use one of the methods described by Bill and David.
Hope that helps
-j
 
Erik Pragt
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
thanks all for the help! I will 'study' (don't know a better word at this time) your answers, and determine which one to take.
Thanks again,

Erik Pragt
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!