• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

page relaod probem- resending data to servlet

 
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to prevent Servlet from sending same data to web page, when user hits
reload button.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See:
DoubleSubmitProblem
 
kwame Iwegbue
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
kwame Iwegbue
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic