Forums Register Login

POST request and reading data two time

+Pie Number of slices to send: Send
How does one forward a POST request when the POST data has already been processed?
+Pie Number of slices to send: Send
You forward with a RequestDispatcher.

The parameter data is available throughout the request (regardless of forwarding) via the methods on HttpServletRequest.

Are you parsing the request yourself for some reason? If not, you don't need to worry about it.
+Pie Number of slices to send: Send
imagine i have read parameter in controller servlet and forwarding to JSP that JSP also need to access request.getParameter.
At this time how JSP will get data from request parameter.

So my doubt is what else i do instead reading the request parameter in JSP?
+Pie Number of slices to send: Send
Well, you shouldn't have any Java code in the JSP, so you should use the JSTL: ${param.paramName}

But even if you did have Java code in the JSP, what makes you think that you can't just use request.getParameter()?
+Pie Number of slices to send: Send
thanks bibeault.... but i am not facing any real coding problem now i just want to clear my doubt. Let's take this scenario....

I am creating a filter to check the contents of a form (POST) request going to servlet. the filter read the form parameter to block unwanted request or check for vulnerable request content. Then the destination servlet reads the request with getInputStream(). If you just do getInputStream() or getReader(), the destination servlet throws an error because you can only read the input stream once.
Here what i do in my destination servlet to manipulate response using form parameter.
+Pie Number of slices to send: Send
Why are you reading the input stream yourself instead of using the request methods? That's what's causing any problems, and is generally not necessary and rarely a good ides.
+Pie Number of slices to send: Send
can i able to read POST request data more than once? i thought once i read POST data the POST data content will be lost, that is why i was putting this scenario to get cleared.
+Pie Number of slices to send: Send
You have not answered my question. Why are you reading the POST data in the first place instead of using the request methods?
+Pie Number of slices to send: Send
if the form body is not in name/value pair format, but only has a value so i am reading in stream and processing.
+Pie Number of slices to send: Send
Ah. In that case, once you read the body, you'll need to pass it along because, as you know, subsequent resource will not be able to read it again. You could store it in request scope and everything later on in the same request will have access to the information.
+Pie Number of slices to send: Send
If you call request.getParameter("parameter-name") in your first servlet, then redirect to a second servlet, you can still call request.getParameter("parameter-name") in the second servlet.

This processing of the form data into request parameters happens behind the scenes, and you can call request.getParameter all you want.

+Pie Number of slices to send: Send
 

Mark E Hansen wrote:If you call request.getParameter("parameter-name") in your first servletan call request.getParameter all you want.


His point is the the POST is not in a format where getParameter() can be used. So he must read and process the POST body himself.

getParameter() will only work for POST data that's encoded as x-www-form-urlencoded.
+Pie Number of slices to send: Send
 

Bear Bibeault wrote:

Mark E Hansen wrote:If you call request.getParameter("parameter-name") in your first servletan call request.getParameter all you want.


His point is the the POST is not in a format where getParameter() can be used. So he must read and process the POST body himself.

getParameter() will only work for POST data that's encoded as x-www-form-urlencoded.


I realize this is what you thought he said and you may very well be right; I wasn't as sure. It sounded to me like he believed that once he read the parameters (via request.getParameter) that he couldn't do it again. I was just trying to clarify that.
+Pie Number of slices to send: Send
 

Mark E Hansen wrote:If you call request.getParameter("parameter-name") in your first servlet, then redirect to a second servlet, you can still call request.getParameter("parameter-name") in the second servlet.



By redirect, do you mean sendRedirect??? If yes, this statement seems invalid.

+Pie Number of slices to send: Send
Good catch! Yes, if a redirect is performed, an entirely new request is generated and the parameters will not be present on that request.
+Pie Number of slices to send: Send
 

Mark E Hansen wrote:It sounded to me like he believed that once he read the parameters (via request.getParameter) that he couldn't do it again.


Yeah, that's what I thought at first too. but then he posted:

if the form body is not in name/value pair format, but only has a value so i am reading in stream and processing.


which indicates that the POST is not in x-www-form-urlencoded format.
+Pie Number of slices to send: Send
 

Bear Bibeault wrote:Good catch! Yes, if a redirect is performed, an entirely new request is generated and the parameters will not be present on that request.


Yes, sorry. I got forward confused with sendRedirect. However, I think even in the forward case, the input stream has still already been read, and so can't be read again. However, if request.getParameter was used in the first servlet, it can still be used in the forwarded-to servlet.
I hope I have that right (this time).
+Pie Number of slices to send: Send
Yes.
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4059 times.
Similar Threads
capture request parameters onunload
Communication with Servlets
Issue with IE 7.0 browser? Unexpected GET request after POST request
what is differance between the get/post method?
travel agent - java writing to file via user input
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 22:53:02.