File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes POST request and reading data two time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "POST request and reading data two time" Watch "POST request and reading data two time" New topic
Author

POST request and reading data two time

sakthi vadivel
Ranch Hand

Joined: Dec 16, 2007
Posts: 41
How does one forward a POST request when the POST data has already been processed?


sakthi SCJP, SCWCD OCA
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61747
    
  67

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.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
sakthi vadivel
Ranch Hand

Joined: Dec 16, 2007
Posts: 41
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?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61747
    
  67

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()?
sakthi vadivel
Ranch Hand

Joined: Dec 16, 2007
Posts: 41
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61747
    
  67

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.
sakthi vadivel
Ranch Hand

Joined: Dec 16, 2007
Posts: 41
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61747
    
  67

You have not answered my question. Why are you reading the POST data in the first place instead of using the request methods?
sakthi vadivel
Ranch Hand

Joined: Dec 16, 2007
Posts: 41
if the form body is not in name/value pair format, but only has a value so i am reading in stream and processing.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61747
    
  67

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.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
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.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61747
    
  67

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.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
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.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61747
    
  67

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

Joined: Jan 10, 2002
Posts: 61747
    
  67

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.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
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).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61747
    
  67

Yes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: POST request and reading data two time