This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Servlets and the fly likes Creating post variables with response object Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Creating post variables with response object" Watch "Creating post variables with response object" New topic
Author

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 ?

Cheers

Nathan
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

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: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Creating post variables with response object
 
Similar Threads
How can a non-servlet find application root and best way to access a servlets data?
struts Action for asynchronous HTTP processing
Passing Vector Object
Handling dynamic configuration data
encodeURL() purpose and best place to use