Passing java objects from jsp page to servlet through html forms - java

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.

Related

Passing Browser Tab information at backend?

Scenario:
We have one analysis which gives different results based on different inputs. So if the user open the same analysis in two different browser tabs, the session variables being common will get overridden and output will be same in both tabs though we want different outputs based on different user inputs in tabs.
So we plan to send a tab-id at the backend so that we save session variables per tab-id.
Is there some automatic way that tab information is being sent to the server like may be in request header or something like that??
Or we will have to generate a tab-id ourselves and send it with every request?
You'll have to generate your tab-id and pass it back with each request, but the following might make it a bit easier:
You can use sessionStorage from Web Storage API to store values unique to each tab. Every tab in the browser starts a new session so they are always distinct.
https://developer.mozilla.org/en/docs/Web/API/Window/sessionStorage
It should work with most common browsers (even IE8+): http://caniuse.com/#search=web%20storage
Hope that helps!

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.

Request multiply parameters

I got a problem regarding request.getParameterMap()
In my jspx file multiply parameters are added when the user selects items out
of a table. now that i want to get them in my Controller
with request.getParameterMap(); it doesnt work with Firefox, Safari
but it works fine with chrome
anyone has an idea why it is so ?
thx for the replies .. the html form with the request parameters is build dynamicly at runtime using javascript, which seems to cause problems with widged based browsers like safari, firefox and most likely IE.
i will use ajax to get it work
The problem is not in the way that you are getting the parameter values from the map. It cannot be. The problem has to be in the HTML / Javascript / whatever that is creating the request on the client (browser) side.
It is most likely that you are doing something that is not strictly HTML (or whatever) compliant. Some browsers are treating it one way, and others another way. I suggest that you start by running an HTML validator over the page.
browser is not aware of request.getParameterMap() or any method in servlet api.
In your use case : you have a table of data , out of which user may select some rows and you need this data in server side for some action on those rows.
For eg : mark as read in GMAIL
select one or more rows in gmail and click ,mark as read will sent some request to google server and get those rows marked as read by user.
You can do it this way, you need to send primary key of your rows to server side with same parameter name
for eg: /delete?delId=1&delId=3&delId=7
and use request.getParameterValues() in server side to retrieve a list of ids to be deleted.

playframework form inputs with array names

I'm trying to submit a form with a few textareas like this:
<textarea name="criticism[]" rows="3" cols="5"></textarea>
The textarea needs to have an array as the name because there can be an unlimited number of them on the page, added by the user with js.
The values are passed to the controller correctly.
In the controller I do params.flash() which seems to add the values to the seession, since if I do ${flash} in the template they are printed to the screen. However, I can't access them in any way. ${flash.criticism} returns null, and ${flash.criticism[x]} will return an out of bounds error.
Am I missing anything syntax wise?
Cheers :)
The flash scope is only available to the current request and the next one. To put something in the session use session.
However flash and session are not intended to store values. They are cookie limited to 4kb. To store something use the db and/or the cache
If you want to re-render your values in the next page, just pass the string array as a 'criticism' parameter to the next render method and use it in your template with ${criticism[x]}

caching the form values

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.

Categories