aspose file tools*
The moose likes Servlets and the fly likes page relaod probem- resending data to servlet 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 "page relaod probem- resending data to servlet" Watch "page relaod probem- resending data to servlet" New topic
Author

page relaod probem- resending data to servlet

kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
How to prevent Servlet from sending same data to web page, when user hits
reload button.


Who dares, wins! (SAS motto)
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

See:
DoubleSubmitProblem


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
Thanks Ben. Very good article by Bear.

I understand the Page Controller, Task Controller example but i'm having some difficulty with the Front Controller technique.

...
The "unit of control" in a Front Controller environment is generally not a servlet at all, but rather a delegate class that implements an interface that the Front Controller uses to manipulate the controller unit. Because these delegate classes represent operations to be performed, they themselves are instances of a pattern known as the Command Pattern and are therefore usually termed Commands.
...


I get the idea based on the Command pattern. The Command classes share an interface. The Front Controller is composed with Command classed. The
url has a string like /command/* that is parsed by the web.xml sending control to the Front controller. In there, an id, which is after /command/* is used to determine which of the Command classes to use. This then delegates to a method from the Command classes, like execute() which has the code for the task to be performed (non-idempotent vs indempotent). I get all this. What I'm not clear about is how this will do Post-Request-Get?
If the user hits reload or back button, won't the same id be sent anyway?

...
The PRG Technique described previously also applies in the Front Controller scenario in which Commands can be Task Commands (typically non-idempotent, operational) or Page Commands (always idempotent, page preparatory).
...


Do you or Bear have any examples please?
[ January 18, 2007: Message edited by: kwame Iwegbue ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Although the Front Controller pattern is not necessary to implement PRG, it is a good complement to it.

What is key to PRG is to understand the difference between Task and Page Commands, segregating the code appropriately between them, and "breaking up" the request into two via a redirect between them.

That way, a refresh will only cause the Page Command to re-execute.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
From the article...

...
* A GET request is made identifying the data to be displayed to a Page Controller.
* The Page Controller gathers the data from the business tier and places it on the request as scoped variables.
* The Page Controller forwards to the JSP page which display the data.
...

* A POST request is made to a Task Controller with data need to complete the controller's task.
* The Task Controller instructs the business tier to perform the appropriate operation(s).
* The Task Controller redirects to the appropriate Page Controller to display the results of the operation, which then follows the steps outlined in the previous scenario.


Does it mean that the configuration code in the Front controller servlet has to differentiate between GET and POST? SO if it is a POST request, delegates to Task command class (which then redirects to Page command class); if it is a GET request , then send to Page command class?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

I guess it can, but that's not what I intended, and not what I do.

GET and POST should be used when approrpiate. While nothing prevents using a GET to submit an operation, or a POST to fetch a resource, that's not the intention of the HTTP protocol.

I have never used the method (GET or POST) to conditionalize what happens on the server.

In my own applications, the various commands will each have unique handles.
kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
Bear I'm sorry, but I really don't get it

url/command/deleteItem comes in with POST.
its mapped to MyFrontControllerServlet
doDeleteTask is called. Data is persisted. response.sendRedirect to showConfirmDelete.

user reloads page.
again mapped to MyFrontControllerServlet.
again doDeleteTask is called. again attempts to delete from database. then redirects to showConfirmDelete.

I thought the point was to avoid calling the Task command repeatedly?
[ January 18, 2007: Message edited by: kwame Iwegbue ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Originally posted by kwame Iwegbue:

user reloads page.
again mapped to MyFrontControllerServlet.
again doDeleteTask is called


That is not what will happen.

Because you redirected to showConfirmDelete, that is the URL that is "sitting" on your browser. And that's the action that will get called again when the user hits reload.

That's what the redirect does for you. If you forwarded from doDeleteTask to showConfirmDelete, the browser has no knowledege of that and will refresh doDeleteTask. But the redirect causes the browser to be updated with the URL of the Page Command.
kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
Thanks Bear. makes sense! Like I said, great article. Call up the GOF. It should be made into a pattern or something: "The Front Man pattern"
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

You're welcome. I'm glad that this (and the article) was helpful.

I can't take credit for inventing the technique/pattern; I'm just trying to promote its use for the benefit of all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: page relaod probem- resending data to servlet