Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Greg Atkinson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic