JSP variables question
April 9, 2004 6:35 AM   Subscribe

I have a JSP Question. How can you pass form variables to another JSP page without using cookies or sessions? I'm doing my form validation on one JSP page and then I need to do the processing of the form in the other. I'm guessing I could set some type of variables like php's $_POST but I'm not sure what to do.
posted by Stynxno to Computers & Internet (8 answers total)
 
You can request the form fields from the processing page, can't you? In ASP, the syntax would look like:

Request.Form("field_name") or
Request("field_name") or finally
Request.QueryString("field_name")

This method returns the value of the form field passed as a querystring using the "POST" method in the form.

That would give you the value of the form field which you can attach to a variable or just use on its own.

I've worked with JSP some, but I can't remember specifically the syntax for this. At any rate, I know for certain you don't need sessions or cookies to do this.
posted by annathea at 6:45 AM on April 9, 2004


request.getParameter("form_field_name");
posted by pissfactory at 8:33 AM on April 9, 2004


I'm not JSP fluent, but in any scripting language I've worked in, you can always submit the form to itself and tip the page off to the form submission (or just test for it if you can do so reliably). So the form submits back to itself, you run your validation there and if everything's ok, you process whatever needs to be processed and then give the user feedback or redirect them.

Is that do-able?
posted by yerfatma at 9:53 AM on April 9, 2004


If I'm guessing correctly, the problem is you want to have three jsp pages:

A.jsp -- contains the HTML form, and POSTs the data to...

B.jsp -- which contains validation code. Assuming the data validates, somehow pass it on to...

C.jsp -- which actually processes the data.

The problem is not how to get raw form data (which is the query that most people here have answered) but how do you pass data from B.jsp to C.jsp without using sessions or cookies. Is that right?

I don't know JSP, however, only ASP and and some PHP, so perhaps, as you say, there is an equivalent in JSP to PHP's $_POST. If there isn't, there are a few (messy) options available to you.

First obvious solution is to remove the problem altogether -- combine the code from B.jsp and C.jsp into one file. However, I'm assuming that, since you're asking this question, this isn't a practical solution.

Another way to do it would be to construct an HTML form in B.jsp, which contains the validated form data in hidden fields, and POSTs to C.jsp. Presumably, the web browser is being sent to B.jsp and then B.jsp contains code to redirect to C.jsp upon successful validation? Well, instead of doing an HTTP redirect, have the web browser submit the form in B.jsp automatically onLoad. Not particularly elegant, mind, but it gets the job done.

I'm also guessing that you don't want the form data slapped into the URL, like "C.jsp?name=John%20Smith&SSN=123456789", because that's one really easy way to pass data around, but also a really insecure way. A variation on this would be to get B.jsp to save the data temporarily to a database record, and then pass the ID of this record in the URL that redirects to C.jsp: "C.jsp?id=47393". C.jsp then deletes this record for security's sake. Again, a bit messy, and also you're effectively making your own kind of session ID, which you wanted to avoid, but perhaps this will work for you.
posted by chrismear at 12:19 PM on April 9, 2004


Response by poster: chris - you're correct on all accounts and i totally forgot about onLoad. I'll try that if I can't find a better solution.

thanx!
posted by Stynxno at 1:08 PM on April 9, 2004


I've done a bit of digging and reading, and while I still really know nothing about JSP, I have seen it mentioned in a few places that <jsp:forward> "invokes the specified page with the same request object, meaning that the new page has access to all the original parameters". (See here.) Perhaps worth a try?
posted by chrismear at 2:03 PM on April 9, 2004


Response by poster: oh nice! that worked just like i wanted!

I owe you a beer some time :)
posted by Stynxno at 2:38 PM on April 9, 2004


Just as a footnote, yet another way to handle this problem might be to use session variables/objects. That introduces a different set of issues, of course - depends on the application.
posted by normy at 11:01 AM on April 10, 2004


« Older What to do in early stages of actors rehearsing a...   |   Do I need a new hard drive? Newer »
This thread is closed to new comments.