This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes how to read post request body in one servlet and then forward request on to another servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to read post request body in one servlet and then forward request on to another servlet" Watch "how to read post request body in one servlet and then forward request on to another servlet" New topic
Author

how to read post request body in one servlet and then forward request on to another servlet

Greg Atkinson
Greenhorn

Joined: Aug 01, 2012
Posts: 2
I'm fairly new at servlets so I was trying to find good examples on the web, or some guidance, about how to create a servlet that can read a POST parameter (in Tomcat 6) to determine which next servlet (in the same Tomcat instance so I am using crosscontext="true") the request really should be forwarded onto. Unfortunately I have no control over the second servlets and I have only the ability to solve this problem via a web app - supposedly filters are out of the question due to some policy issues but I understand I would probably have the same question if I was creating a filter. Basically I am trying to correctly understand all the ins/outs of reading the request body multiple times and how I can do that in the first servlet without touching the second servlets, which use a getInputStream() on the request. Originally I attempted the first servlet using a getParameter() and the second servlets failed because the request body was not there... good learning experience but now I am a little lost at how I can do this. It looks like I have to wrap the request and use the wrapped request to read in the first servlet and maybe forward the wrapped request as well but not sure exactly how this is done so I am looking for some good examples, tutorials, or guidance. Thanks!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
I don't think you can do that with a normal servlet - once the request body is read it can't be "rewound" - a Filter could read the body, keeping a copy to supply as a stream to the chosen next servlet.

Bill



Greg Atkinson
Greenhorn

Joined: Aug 01, 2012
Posts: 2
Bill, thanks! The idea that a Filter could do this has lead me down some interesting learning paths. One question that comes to mind - if a Filter can keep a copy, could not another servlet? I've tried this (and hit many snags) but it seems theoretically feasible based on documentation:

1. Create a class (called WrappedRequest here) that extends HttpServletRequestWrapper and overrides a few methods like getInputStream and getParameter using a copy of the request body that the constructor reads into a byte array
2. Create a new WrappedRequest from the doPost request
3. Process the WrappedRequest in the first servlet
4. forward the WrappedRequest to the second servlet
5. the second servlet theoretically seems like it'll have access to the request body if I do the overrides correctly

Not 100% sure but still learning here... thanks.
 
 
subject: how to read post request body in one servlet and then forward request on to another servlet
 
Similar Threads
404 - the requested resource is not availabe...
request
Java client querying web page
Clarification on servlet based web service end point.
Having trouble getting Tomcat up and running