Richard Stedham 3343 wrote:
>
> I am having the following problem and would appreciate some help/advice.
>
> 1.  An HTML form collects information and passes this across, using POST, to a
>     servlet.
> 2.  The servlet adds the information to a database, sets up a bean and uses the
>     requestdispatcher to forward the browser to a JSP page that displays the
>     new information.
>
>     No problem so far
>
> 3.  If the users reloads the page then they are asked if they want to repost
>     the form data. If they say yes it adds the information to the database
>     again!
>     The servlet URL is the 'active' URL after the request dispatcher has been
>     used.
>
> I could use the request.forward put I want to be able to pass a bean.
>
> Can anybody tell me how to get around this problem ?

When you use "forward", it's similar to a function call in a program; from the
outside it's impossible to see that the servlet/JSP you invoked initially is
using another servlet/JSP to do it's job. And the consequence of this is of
course that the browser still "points to" the initial servlet/JSP since it's
the only URL it knows it has requested.

There are basically two ways to avoid the problem you see:
1) Tell the browser about a new URL that can be reloaded without causing a
   new database access. One way of doing this is to save the bean in the
   session scope instead of the request scope and redirect to the JSP page
   instead of using forward. One drawback with this approach is that it involves
   a client/server round-trip; the client is told to make a new request.
   Another possible drawback is the usual problem with sessions; if the user
   has multiple windows open belonging to the same browser process and submit
   forms with different contents, the bean stored by one request overwrites the
   one stored by another so the JSP page may display data in one window that
   you'd expect to be displayed in another window. See the archives for more
   info on this; it's been discussed at length many times. If it's unlikely
   that a user submits two or more requests from different windows at roughly
   the same time, this may not be a problem at all.
2) Keep track of the session state, so that you can tell the user that the
   data has already been processed if the form is posted a second time.
   This can be done by adding a flag in the session scope and let the
   servlet look at it before storing the info in the database. If it's set,
   you can forward to another JSP (or static HTML file) saying "Sorry, you
   can only do this once per session." This solution of course only works if
   it's correct that the user can only perform this operation once per
   process, or if there's a way to reset the flag. A variation of this theme
   is to use the database info itself as the flag; check if the submitted
   data (e.g. a primary key) already exist in the database. If so, reject the
   request as above or update the info, depending on your application needs.


--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to