views:

220

answers:

1

Hi all, I have a jsp search page (Search.jsp) and a result page (Result.jsp), both of them can choose search criteria. and then passed the parameters to a java controller file (Controller.java) to build a query string and performs query searching. The query string and searched results will be passed to Result.jsp for displaying.

Currently I use servletContext to remember the processed query string, and if users use Result.jsp to select search criteria, Controller.java will append such criteria to the existing query string. If I do a few searches using Result.jsp. For example, query string would display ((Query1) AND Query2) AND Query3 on the Result.jsp page. Then using the browser's back button to go back to the previous display page. For the same example, query string displays (Query1) AND Query2. Then if I do search again. The query string (((Query1) AND Query2) AND Query3) AND Query4 would be used. I know this is expected with my current implementation since Result.jsp does not do any modification with the processed query string.

However, I would like when user uses the browser Back button, for example, query string displays on the page as (Query1) AND Query2, and perform search, the query string should be ((Query1) AND Query2) AND Query4 in which the query string is build based on the current displayed query string on the Result.jsp page plus the current selection. How can I do that? It sounds quite simple but I have tried several ways of using the in Result.jsp to update the query string, but still couldn't get it right. Therefore I am wondering maybe my approach of using <c:set> is wrong. I would like to hear your suggestion. Thanks in advance.

+2  A: 

Currently I use servletContext to remember the processed query string

Do you realize that the ServletContext is shared among all users/sessions who are visiting your webapplication? Once visitor X modifies it, the changes are reflected for all other visitors. Don't you rather want to store it in the HttpSession to keep the data visitor-specific?

See also:


I want to use back button for some reason

I really have a hard time in understanding your functional requirement (the some reason part). But at least, since you'd like to have the back button to work properly, you'd like to use idempotent requests here. So, where applicable, replace POST by GET.

Whenever you'd like to refire a real HTTP request on the server instead of loading the page from browser's cache, you'd like to instruct the browser to not cache the pages by adding the following headers to the response:

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setDateHeader("Expires", 0); // Proxies.

You can do that in your controller or some Filter covering the url-pattern of interest.

See also:

BalusC
Hi Balus, thanks heaps!! You always give me more than I asked for. Thank you so so much for correcting the hidden bug of using ServletContext!! I need to do more reading on this then.Regarding the GET and POST, I thought POST is just to allow more parameters to be passed to servlet. In my case, the query string can be very long so it can possibly exceed the max length of GET. Comparing to the idempotent concern, I think exceeding the GET max length is more serious. I may still stick to POST then. What do you think?
Kenneth
If you stick to POST, you won't be able to refire the request when going back in browser's history without bothering the enduser with safety warnings (e.g. a "Are you sure to resend the data?" confirmation or possibly "Page Expired" error). I'd just limit the search string length to 2K characters. That's already pretty much. Google also just uses GET.
BalusC
Ok........ I take your advise to use GET then. Thank you very much!!
Kenneth
You're welcome.
BalusC