aspose file tools*
The moose likes Servlets and the fly likes 'POST'ing parameters to another JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark " Watch " New topic
Author

'POST'ing parameters to another JSP

Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
I hope this is in the right section - it seems that all the other discussion on this subject is in this section. I have searched for the info, founds lots of threads, but not found a definitive answer !

OK ....

I have a JSP page that presents the user with a form (its actually a JSP component used within a portlet).
When the user submits the form, the action of the form is another JSP which takes the form POST'ed values (with request.getParameter...), and inserts them into a database.
When this is complete, I want to transfer the user back to the page they came from (the one with the form). I identify this page with request.getParameter("referer")
I want to pass back to the "referer" page a parameter which is the status of the database insert - the idea is that if the returned paramter indicates success, display a thank you message and not re-display the form. Otherwise, display an error, redisplay the form and invite them to try again.
You get the idea.

Everything is working fine except the passing parameters back via POST (I do not want to put the parameter on the URL - IE a GET request - as I dont want to user to see any of this - it should be seemless)
My original code within the 2nd JSP to send the user back to the calling page was
This redirects back to the page no problem, but I cant work out how to pass a parameter which I can pick up with request.getParameter

Out of sheer desperation I've bodged it like this:

How nasty is that !!!
Whilst it works - it redirects back to the calling page, with a parameter passed as POST - its really nasty code !
There must be a better way - surely ???

Any ideas on this (with code samples ) would be very much appreciated,

Cheers

Nathan
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Did you try using the RequestDispatcher object?


Alco-Haul: We move spirits.
Demented Deliberations of a Dilettante
Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
Sharad,

RequestDispatcher .... hmmm, thats what most of the other threads were leading towards or suggesting.
Trouble is, I dont know what RequestDispatcher is or how it works !

I've looked up the Sun Javadocs for it, and I still dont understand !!

Can you explain RequestDispatcher, and possibly give example code of what I should be doing ??

Many thanks

Nathan
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
RequestDispatcher is a servlet interface. It is described here.

The idea is fairly simply. Invoke the getRequestDispatcher() method on your ServletContext object with the URL for the target JSP. The invoke the forward() method on the RD.

Hopefully the APIs should be adequate for you to work it out. If you still have trouble, let us know and we will provide some sample code.

Regards,
Sharad
Demented Deliberations of a Dilettante
Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
Sharad,

Many thanks for your replies - the link you provided is one of the many pages I have tried to understand about RequestDispatcher - I'm getting there !!

OK, so I've managed to get the method forward (and include for that matter) working - but - and this is the big problem - it changes the URL to that of the jsp that process the form and inserts the record into the database.

EG. The user goes to my site - the address is http://host/homepage.jsp - and that is what they see in their browser URL bar.
This page displays the form.
When the user submits the form, the action of the form is a jsp called submit.jsp
When this file has finished processing, it should return to homepage.jsp passing a parameter.

Using RequestDispatcher and the forward (or include) method, I can get it to return to the homepage.jsp (and pick up the parameter - not tested that part yet !!) - but the URL in the browser URL bar is still submit.jsp and not homepage.jsp

Any ideas how I can get round this ??

Or should I take a different approach to processing the form in the first place (like do it all in homepage.jsp ??)

Cheers

Nathan
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Ok. So briefly this is the scenario:
1. User goes to /homepage.jsp and fills out a form with target /submit.jsp
2. submit.jsp does its magic and dispatches the request to /finalTasks.jsp
3. User gets the appropriate response page but address bar has /submit.jsp

And you want the address bar to have /homepage.jsp. Is this correct?
Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
Almost correct.
The scenario is:

1. User goes to /homepage.jsp and fills out a form with target /submit.jsp
2. submit.jsp does its magic and returns the user to /homepage.jsp
3. User gets the appropriate response page but address bar has /submit.jsp

And you want the address bar to have /homepage.jsp. Is this correct? - Yes

IE. no /finalTasks.jsp or other intermediary page.

The requirement is that the user ends up back on the homepage with /homepage.jsp in the url bar

Most importantly, submit.jsp has to pass a parameter back to homepage.jsp so that homepage.jsp can display "Thanks for your input", or "There was some error ....." - you get the idea

I can redirect from submit back to homepage with response.sendRedirect - and this shows the correct URL - but I cannot work out how to pass parameters as POST parameters.

Cheers

Nathan
[ May 27, 2005: Message edited by: Nathan Russell ]
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
As far as I know, the address bar value is determined by the browser. So, if you make a request for submit.jsp (via form submission), the address bar is updated. It will always have the value from the last requested URL (browser side).

Hence, your little hack worked as the browser's last request was for /homepage.jsp. I am not aware of any way to instruct the browser to set a particular URL in the address bar.

One possible solution is to make /homepage.jsp the Controller (MVC framework). It receives all requests, but then it routes to the appropriate handler (e.g., submit.jsp). That way, the browser always requests /homepage.jsp with different parameters and the address bar remains at /homepage.jsp.

I am wondering though, why would you care what the address bar looks like?
Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
Sharad,

Thanks for the info and the responses - its been an interesting discussion.

I think my solution (for now) will be to leave my nasty little hack in place.
When I get some time I think the best solution would be for the form on the home page to call itself (ie. the form on the homepage submits to the jsp that is the homepage !!!) - where with a bit of logic it can pick up the params, post to the database, and display the "thank you" or whatever accordingly.
This way there is one less request/response so network/server traffic will be lighter; and one less jsp for me to write, support and maintain.
I am wondering though, why would you care what the address bar looks like?

I think the address bar is very important. Not only does it have some 'cosmetic' value - there is functional value too. What happens for example if the user goes to the homepage (/homepage.jsp), submits the form, the homepage is redisplayed (but as /submit.jsp) - and then the user decides to bookmark the page ?

Thanks again for your input and a very interesting discussion,

Cheers

Nathan
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Originally posted by Nathan Russell:
When I get some time I think the best solution would be for the form on the home page to call itself (ie. the form on the homepage submits to the jsp that is the homepage !!!) - where with a bit of logic it can pick up the params, post to the database, and display the "thank you" or whatever accordingly.
This way there is one less request/response so network/server traffic will be lighter; and one less jsp for me to write, support and maintain.

When you do proceed in that direction, do also take a look at Apache Struts. As the application starts growing in size, a formal framework will go a long way in maintenance and extension of the codebase.
The hack has two major problems:
1. It incurs an extra server network trip
2. It relies on an HTTP redirect

Another shorter term option to consider would be to still retain the /submit.jsp processing in a separate JSP that is included in /homepage.jsp

Originally posted by Nathan Russell:
I think the address bar is very important. ... and then the user decides to bookmark the page ?

Fair point. We should try to allow bookmarks to work as far as possible.

Glad there was some value from the discussion. Good luck.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 'POST'ing parameters to another JSP