Problem with Request object

In my JSP page, I have a text box and button. When enter product name and click button, the request goes to servlet, product is created in the database, page redirected to JSP with RequestDispatcher. The problem is after creating the product, if I press F5 again, the servlet is called and tries to create the product again, which is just before created. i.e. I type "MPX101" and click button, I get alert message "MPX101 is created" and textbox is empty (obviously). I press F5 and I get an alert message "MPX101 already exists".

The same thing happens with another page for delete. If I press F5 immediately after deletion of an item, again same item is being tried to delete.

When I traced request object, still it contains product name, which I created already. Can we clear contents of request (after creating of product and JSP is loaded, in my case) ? Any sort of help is greatly appreciated. Thanks in advance.

[937 byte] By [baskarka] at [2007-11-27 3:56:03]
# 1

You say,

> redirected to JSP with RequestDispatcher.

What I understand from this is that you're using the .forward() method of the RequestDispatcher. This is not the same as request.sendRedirect().

With forward(), you're sending the original request object that the servlet received as the request for the JSP page. That is why you see the original contents in the request, because it is the original request. When you press F5, the browser resends the same request to the servlet again ( you're probably getting a popup warning you about resubmitting in case you're buying something or something like that, atleast in IE ).

What you should do is use the request.sendRedirect() to ask the browser to request for your JSP again and not use the forward() method which forwards the original request, which is asking your servlet to enter the product into the DB. You will not need to clear the request as it will be destroyed after it has been sent once ( unless you forward it like you were doing in this case! )

nogoodatcodinga at 2007-7-12 9:00:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Indeed. Summarized:

RequestDispatcher#forward() just replaces the current request with another request. This is totally transparent to the end-user. You'll also see that the URL is not being reflected.

HttpServletResponse#sendRedirect() actually invokes a HTTP302 redirect to another URL. This is visible to the end-user and also the URL is reflected.

BalusCa at 2007-7-12 9:00:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...