File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Creating post variables with response object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Creating post variables with response object" Watch "Creating post variables with response object" New topic

Creating post variables with response object

Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
I wonder if anyone can offer some advice with this, or offer some other creative solutions !

I have a servlet which once processed needs to return the user to a given JSP page passing with it some variables.
I am currently using response.sendRedirect to send the user to the next page. I can pass my variables as GET querystring variables by appending them to the URL; but I dont really want to do that as I dont want to visually expose such things on the URL (I like neat URLs !!), and my target JSP has currently been written to handle POST variables (but I guess I could re-write that section if needed)
I could pass my variables as session variables, but again, I would need to re-write a couple of sections of my target JSP to pick them out.
Given that my JSP file is currently coded to read and respond to POST variables, I'd really like my servlet to pass variables to it in that way.

Is there a way to do this ?, or some other creative solution ?

The only 'creative' solution I can think of (and I dont like it) is to create a PrintWriter object from the response object within the servlet, and using the println method, create a HTML form whose action is my JSP file and method is post and which has hidden input fields being the variables I want to pass. Lastly, I output a bit of JavaScript to submit the form !
Yuck !

Any better ideas ?


Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

A redirect always elicits a GET request from the client. The HTTP protocol defines that. So you can't make it a POST. But why not just store your data in the user's session?
Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
Yeah, I can see how session variables will work for me, but I'll need to re-work some of the JSP to pick up the session vars - its already written to pick up the POST vars (which will need to stay as other JSP pages submit POST vars from a HTML form to it); so I will need to add code to make it read and process session vars as well.

I guess my hope was that I could send POST vars from a servlet, and therefore not have to change code on a page that already works

Never mind !!
I agree. Here's the link:
subject: Creating post variables with response object
It's not a secret anymore!