views:

380

answers:

3

I want to set the current form object as session parameter before submitting the form.Is it possible?

<html>
<head>
Title 
</head>
<body>
<FORM method="test" name="test" enctype="multipart/form-data" action="sample.jsp" >
<select name="test" id="test"> 
<option>1</option>
<option>2</option>
</select>
<input type="submit" value="Submit">
</form>
</body>
</html>

I want to set the select option test in the session before submitting the form.Is it possible.I can't use request as there are some flaws in multipart implementations.

A: 

I thing and feel no and its not good idea at all. Sessions should always be done on server side. So whn the request comes to the server you can do all session related stuff.

asb
A: 

You could use Ajax to talk with the server. I do not understand if you want all the form in the session or just the value for the test combo?! If it is just the value for the test combo then you can get that from the page and submit it asynchronously (whenever you want) to the server to some action that receives the request, extracts the test value and saves it in the session. It will be there when you submit the form the normal way.

Using Ajax is a solution, but it is a complex one to something that might be a simple operation. As the others said, it is not a good idea.

Why would you want this behavior? What is it exactly that you want to do?

dpb
Ajax is costly.The problem is already posted herehttp://stackoverflow.com/questions/1305329/getting-error-when-using-post-method-in-formI did not get enough replies :(
You can't read the parameter because the server does not understand the request because of the "multipart/form-data" encoding. Remove enctype="multipart/form-data" from your form declaration and test the pages again. You should be able to read the value.
dpb
Exactly.The problem is I forgot to attach the file element in that sample form .That file Input type was the reason for the enctype
+1  A: 

I can't use request as there are some flaws in multipart implementations.

Then your actual problem is that you're using a poor implementation. The multipart/form-data request encoding isn't by default supported by the Servlet API, you won't see anything in the request parameter map. To retrieve the uploaded file and the other request parameters, you need to parse the InputStream of the HttpServletRequest yourself. But fortunately there's a commonly used API which can take the precious and tedious work from your hands: Apache Commons FileUpload. At their homepage you can find lot of code examples and important tips&tricks in the User Guide and Frequently Asked Questions sections. Read them carefully. Basically you just need to get the InputStream from the FileItem object and write it to any OutputStream to your taste using the usual Java IO way. You can even use the shorthand FileItem#write() for this.

You can if necessary also write a Filter which makes use of Apache Commons FileUpload under the hood and checks every request if it is multipart/form-data and if so, then put the parameters back in the request parameter map with help of Commons FileUpload and put the uploaded files (or exceptions) as request attributes, so that it's finally a bit more transparently in your servlet code. You can find here a basic example to get the idea.

Summarized: don't invent workarounds/hacks to "fix" this problem. For sure don't use Ajax to set request parameters in the session while they are sent to the server side at any way. Just use the right solution for the problem. Use a good multipart/form-data parser.

Hope this helps.

BalusC

related questions