tags:

views:

359

answers:

3

I have a jsp page in which i pass information to 2nd page where i display info in 2nd page using the info passed from the ist page.The problem arises when i use the browser back button.How can i make my browser back button to have updated and refreshed data

A: 

I m not sure, but you can try following options.

1)store the form data in session variable and keep upading in next page. So that when the user hits back (the page loading will happend) button the page will load with the session variables new data.

2)http://www.servletsuite.com/servlets/backtag.htm

3)http://www.theserverside.com/discussions/thread.tss?thread_id=29853

sap
+1  A: 

The only way is to use HttpSession for this -so that the data is available during the entire session-, in combination with post-redirect-get pattern and disabling of the client side cache -so that the back button doesn't request the page from the browser history/cache, but instead requests a brand new one from the server

Here's a kickoff example how page1.jsp is supposed to look like (note the meta headers, those are the minimum set to disable the browser cache the crossbrowser-compatible way, you should not change them in any way):

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2175119</title>
        <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
        <meta http-equiv="Pragma" content="no-cache">
        <meta http-equiv="Expires" content="0">
    </head>
    <body>
        <form action="page2" method="post">
            <input type="text" name="foo" value="${page1.foo}">
            <input type="text" name="bar" value="${page1.bar}">
            <input type="submit">
        </form>
    </body>
</html>

In the servlet class behind page2 you need to collect the request parameters in a data structure like a Map<String, String> or a fullworthy Javabean class which you put in the session by HttpSession#setAttribute() with page1 as attribute name (so that you can access it in JSP EL by ${page1} as demonstrated above):

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Map<String, String> page1params = new HashMap<String, String>();
    page1params.put("foo", request.getParameter("foo"));
    page1params.put("bar", request.getParameter("bar"));
    request.getSession().setAttribute("page1", page1params);

    // Do remnant of your business logic here if necessary.

    // Now redirect to page2.jsp (else the back button won't work,
    // because the POST has been expired due to the meta headers).
    response.sendRedirect("page2.jsp");
}

In page2.jsp you can access the form data the same JSP EL way as in page1.jsp:

...
<p>Entered value for "foo" is: ${page1.foo}</p>
<p>Entered value for "bar" is: ${page1.bar}</p>
...

If you press the back button in page2.jsp, it will fire a brand new GET request to the server and retrieve page1.jsp from the server, with the data from the session prefilled.

This all has however one caveat: if you have multiple browser windows open inside the same session, then the entered/changed values will affect each other. This is not workaroundable as long as you insist in using the browser back button for navigation instead of a real form button in page2.jsp.

Hope this helps.

BalusC
A: 

I think the essence of the question is: how can I make the browser re-request the first page after the user presses 'Back' rather than display the old, temporarily-cached version of the page?

The answer would be to make the result of fetching page 1 uncacheable. This is done with HTTP response headers, and a quick and dirty way to set those headers in a JSP is:

<%
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
response.setDateHeader ("Expires", 0);
%>
Sean Owen
It's preferred to do so in a filter/servlet or using HTML meta tags. Besides, this set won't work in Firefox and few others. Oh, this also doesn't answer his question of retaining new data at all. See my answer for the correct approach and the correct headers.
BalusC