caching the form values - java

I have a search form.When user post the search form i take the user to the result page which has results of the search.Now if the user click on the back button of the browser ,i want that he should go to the search page form filled with his values.I am using java

Use cookies.
when the user submits, just store a json or other simple serialized string needed to configure your search form.
Then on the search form page build out, just check for the existence of the cookie, if no cookie, no value, etc.

Related

Passing java objects from jsp page to servlet through html forms

I have a scenario in which I pass the list of objects through jsp's, its one of the attribute(name) is displayed in the drop down menu,now I want only that object should be passed again to the servlet which is selected in drop down menu while submitting the form.
What will be the best way to do it? As in forms everything is passes as string and get(through request parameter) as strings only?
I know i can pass the name and then in the servlet I can check for that name in the list of objects.But there must be some alternative solution and I am looking for that
Here are the alternatives:
Convert the values into strings and embed them in the HTML etc that gets sent to the user's browser. When the user "submits", send the strings back to the web server as request parameters where the servlet can turn the strings back into values.
Put the values into the request's Session object, and have the servlet fish them out.
Pass the values to the browsers via set-cookie, and then have the servlet extract the cookie values out of the request.
Of these, the Session approach is the most secure. If you put the values into cookies or the web form, then they can be read by the user and (maybe) changed.
You could pass a "name" I guess ... but what you are doing is reinventing the wheel of the Session object.

Displaying results on same page

I am new in servlets, I would like to display results in the very same page that I am on when I click a search button, then the results should be on the very same page, how can I achieve that without going to another JSP or if I am supposed to do it behind the scene moving to another without the user noticing that its another page, how do I make it seem as if its the same page with results on it. Any shed of light is highly appreciated.
You have to use javascript to receive the search results from the server. This technique is called ajax. Javascript libraries like jquery (or many others) can help you a lot with this.
Your form submits the search term to the current page, i.e. you can leave the form action attribute empty.
In your servlet check whether a search term has been submitted. If this is the case, execute your search function and write the result to the response.
You don't mention where you data is coming from, but that is fairly unimportant in this question. The data could come from a local variable, from DOM storage or have been served to you using AJAX. But here is an example of setting text data in a textarea element (there are nearly unlimited ways to format what you want, this is one example only). Dynamically changing your current page in this manner means that you do not need to navigate to another.
It works by getting a reference to the element by it's name ("data" in this case) using document.getElementById
We then set the element's content value to your data, and it is displayed in the textarea.
HTML
<input type="text"></input>
<div>
<textarea id="data"></textarea>
</div>
Javascript
var data = "Here is your data that was returned from your source";
document.getElementById("data").value = data;
On jsfiddle

In a Struts Action, can I write out a byte array as well as redirect the page? (Struts 1, Java)

I'm working on a project in which, after the user submits a form, I'd like to have them download a PDF file (filled out with information from the form) as well as redirect them to another page. I know how to redirect (returning an ActionForward which is set up to redirect to the correct JSP) and I know how to send the PDF (I build it using iText library, then write the byte array out to the HttpServletResponse's output stream), but I'm not sure how to do both without some sort of kludge.
I'm guessing I'll have to do one of the two using javascript/ajax, but I was wondering if there is a more elegant way to do this with struts.
Not with servlets.
What I end up doing in these situations is request the file with a timestamp as a request parameter.
When I write the response I set a cookie with that timestamp. Every few moments on the client side I check to see if that cookie exists, if it does, I window.location to the 'redirect' page.

Displaytag / Spring MVC pagination

I have a Spring MVC based JSP page which uses Displaytag to display a set of data. In the form on the page, the user can select either, or both of two checkboxes to display which subset of the data they want to see (Set "O", set "Q", or "B" both.)
The form and the display work fine on the first page, but the Displaytag pagination links at the bottom of the table contain malformed URLs. The result is that when the user clicks a link to display a different page, the checkboxes are reset and the user always sees "Both O and Q" even if they only wanted to see "Q" (for example).
I have added this parameter to the form that Displaytag uses to generate these URLs (according to the Displaytag documentation) but the value in the generated URLs does not change.
The form entry (type="o"):
<input type="hidden" id="type" name="type" value="o">
The pagination URL (type="b" for both):
http://localhost:8080/searchmain.html?d-443691-p=3&endDate=&_stateQuote=on&type=b&stateOrder=true
Can anyone shed any light on this?
Does anyone know how the URL parameters are generated?
Found the answer - posting it here so hopefully it will help someone else.
Displaytag generates the URL parameters for its pagination using the parameters on your initial HTTP REQUEST, not the RESPONSE. So if you are depending on some updated parameters coming back (as was this case), then your paging will produce incorrect results. In this case, the two checkboxes on the page were being used to update the hidden "type" field which was then passed back in the response. Unfortunately, the paging URLs were updated with the "type" field from the REQUEST which still contained the previous un-updated value (example "B") instead of the new updated value "O".
Whatever values you are hoping to find in the URL will have to be on the HTTP REQUEST you pass in, you cannot expect to utilize anything from the RESPONSE.
Makes sense, it just took a while to find it.
If you want to see the actual code, it's in this routine in TableTag.java in the Displaytag source:
protected void initHref(RequestHelper requestHelper)

How to start a new session if a user click on the new tab in IE or mozilla on websphere portal?

I have one "user search" portlet on the home page of one application running on websphere portal server which displays the matching user records as per the search criteria filled in the search form. I have a requirement to have a "back to search input" link on the results page which on click should show the filled form on the input jsp.
The issue I am facing is if I open the application in two different tabs of same IE browser and start giving some search criteria and submit and at same time search for some other input from the other IE tab (in the same browser) and then go back to previous tab and click on "back to search input" link, instead of showing me the first input it will show me the input I entered in the next IE tab.
I am setting and getting the bean (form bean) through portlet session. But in the two diff tabs of the same IE it will be the same user session (and may be the same portlet session.)
How can I fix it so the two tabs save their search results independently?
The one thing to note here is I can access this "user search" application without logging in. So it must be taking the default portlet session in this case. What will happen once I login and then search; will it overwrite the portlet session and http session?
Basically your issue stems from the fact that your session is the same in both tabs so any data you store you can't expect to be different between tabs unless you store it as such. I would assign each search an id and store them in a collection in your session. Then when you draw your "back to search input" button add the search id such that you can pull the correct search out when building the input page.
Alternatively you could save server memory by posting the search as a query string on the return to search link rather than storing it in session.
What you describe is a fairly common problem faced by web app -- I think the official term is conversations -- which is the need to support something in-between the session scope and the request scope.
Some web framework support this concept, some not. The portlet specification does not support that out-of-the-box (Maybe there's something specific in websphere though).
I think one way to re-create the support for conversations, is to store a conversation id in the HTML generated. This way you can have an ID that is be per-tab / window. If suddenly a request receive an old ID, you know it comes from either an old page (if the user used the back button), or another tab.
That's ony a rough sketch. I'm pretty sure there a good resources on the web about this problem, otherwise spawn another question.

Categories