Forums Register Login

page relaod probem- resending data to servlet

+Pie Number of slices to send: Send
How to prevent Servlet from sending same data to web page, when user hits
reload button.
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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"
+Pie Number of slices to send: Send
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 promise I will be the best, most loyal friend ever! All for this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1214 times.
Similar Threads
How to read and write text file generated by java code in HTML file?
Unfresh the servlet?
How to get the servlet's requesting page ?
Servlet to Applet
How do I call aspx from a servlet?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:18:58.