Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Creating post variables with response object

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 27290
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !!
 
reply
    Bookmark Topic Watch Topic
  • New Topic