This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes design issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "design issue" Watch "design issue" New topic
Author

design issue

Raj Bhandari
Ranch Hand

Joined: Oct 19, 2005
Posts: 97
Hi everyone,
Please guide me whats the best way to do this in jsp/servlets.

I have a page1.jsp
I have a submit button on this page.
Onclick of that button, I need to call page2.jsp
Within page2.jsp, I am doing some processing say insert and give a "task complete" message on page2.jsp.
This works fine.
Now the problem is if the user refreshes page2.jsp, that task gets processed again. How should I prevent this ?[

I tried doing page1.jsp ->servlet1.class ->page2.jsp
but again its the same problem when they refresh because the url shows the servlet rather that page2.jsp.
Any suggestions what I can/should do ?

Thanks,
Raj
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
One way to do this is to have one servlet/jsp doing the action, and then redirect rather than forward to your "success" page.

This tells the browser to issue a new request to "success.jsp"
If they refresh this page, it will just refresh the request for the success message - not the actual operation.

Of course they could push back one page, and then refresh - but it does make it a bit harder for users to screw up your application.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

Rather than forwarding from the servlet to the JSP, use a redirect.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

<Stefan and Bear bump heads> Ow!
Raj Bhandari
Ranch Hand

Joined: Oct 19, 2005
Posts: 97
Thanks a lot for your answers. It solves my problem.
Raj Bhandari
Ranch Hand

Joined: Oct 19, 2005
Posts: 97
The only issue with the redirect is that now I can't send information to the redirected jsp page from my initial jsp/servlet, right ?

How do I go about now showing in the redirected page something like "x rows were inserted" keeping in mind the fact that the processing was done by the initial jsp/servlet ?

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

You can send it as a parameter, store it in the session, submit it as a hidden value, and so on. But you cannot use a request-scoped variable.
Raj Bhandari
Ranch Hand

Joined: Oct 19, 2005
Posts: 97
Hi Bear,
I'm still confused. Could you please guide.
If I'm using this:

response.sendRedirect("finaltks.jsp");

How do I send parameters now ?
I've always like the hidden box, submit button concept to pass on values but how do I send them for a redirect ? Is it possible ? Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

Place them on the query string.
Raj Bhandari
Ranch Hand

Joined: Oct 19, 2005
Posts: 97
oh yes, thats smart
Thanks Bear!
 
wood burning stoves
 
subject: design issue
 
Similar Threads
populate the form field with the submitted input
redirecting page
Display messesge wait during background processing
Liferay Portlet with JSF Portlet bride-Problem
Inter portlet communication