Prevent resubmit a form and struts - forms

I have two jsp page: page1.jsp with a form and page2.jsp which displays a message to tell the user if the record insert happened or not.
If the form is successfully submitted and the user refreshes the page2, another insert happens.
I wouldn't to use a session variable, and setting a request attribute in the page2 doesn't resolve the issue (it seems to not work).

You can give a try to this:
From page1.jsp set a request attribute, when control reaches to
page2.jsp check for that request attribute if it is there redirect the
control to same page2.jsp. In this way when control is redirected to
same or other resource, previous request and response objects are no
more there(i.e. both request and response object will destroy because
HTTP is stateless protocol) And like this after redirection there will
be no data related to previous request and on refreshing the page2.jsp it
will not repeat the previous task.
I am not sure but in one of my application I used the same for the purpose. Let me know if anyone has a better alternative.

Related

How to deal with URL in browser which is changed to form action value after wrong data is submitted?

The question is generally language/framework agnostic but if it matters I work with Grails and most interested in grails specific solution if such exists.
There's a form mapped to URL: /foo/create. When user type in this URL to his browser the form is shown.
Form action attribute directs to /foo/save and has method POST. If saving is successful, then standard post-redirect-get pattern is applied, and user is redirected to /foo/show.
But, if user specified incorrect data, they should see the same form again with error messages and all their data preserved. To implement this behavior, I do forward to the controller which produces the form (the same is mapped to /foo/create).
After that user sees the form with data and error messages, but URL field is changed in browser to /foo/save. And if user change focus to URL field and press enter - 404 will be shown (because nothing is mapped to /foo/save + method=GET pair).
The long story short: URL /foo/save is shown in a browser (as there were no redirection after form was submitted) but it directs to nowhere if accessed by HTTP GET method.
How to deal with this situation? Surely, I can map something to /foo/save but I wonder if there's a way not to change URL shown in a browser after form with wrong data was submitted?
Two approaches:
The form submits to itself, i.e. /foo/create submits to /foo/create, only if successful the page is redirected to /foo/show. This should use a post-redirect-get cycle as well and store the submitted data in the session, but could be a simple POST without redirect.
/foo/save always redirects again, either to /foo/create if the data was invalid or to /foo/show if the data was valid. This will always use a post-redirect-get cycle with the data saved in the session.

asp.net MVC QueryString

project type is MVC2. Let say that i have page1. after success it write somethink to row and get new inserted row id and redirect to another page and sends row id as parameter. and user can see this parameter on querystring. and can change it. i think so taht it is problem in some situation(pages). i use for it a hidden input and after post checking parameter from query string with hidden input value. if they are not equal then writing in to log and redirectiong to error page. does my way is correct. or have a good methods.
thanks...
Exposing IDs like this is pretty standard and is what lets browser bookmarking of specific items work. Your job is to ensure that the user can only see and modify records that they should be able to.
If the user does some URL-hacking and enters the URL to an item they are not allowed to see or modify, you can either just kick them back to the parent page, or give an Access Denied message, depending upon the app/context.
The bottom line is never trust user input, including hidden form parameters.

Classic ASP form doesn't post on page refresh

I have an ASP page that takes two arguments on the querystring. On the page is a form that posts back to itself.
If I do the form post once, and then try to refresh the page, it doesn't repost the form. It loads the page as though it were loading for the first time, missing the querystring values.
Is there a way to ALWAYS force a repost when I refresh a page that is the result of a FORM post?
It sounds like the problem you're having is loss of some essential parameters to your page when posting. In ASP there are two primary methods of passing parameters, in the url string via GET or from a form POST. The former passes you values in the QueryString dictionary while the latter gives them to you in the Form dictionary. Fortunately for you it is possible to accept a parameter that exists in EITHER dictionary by looking to Request object:
Request["a"] will find a regardless of being in Request.QueryString["a"] or Request.Form["a"].
This will help you in your current dilemma because you can simply write your querystring parameters to your Form on initial load of the page as <input type="hidden" fields. On subsequent posts your Request["a"] search for your parameters will find them regardless of being passed in the URL (on initial load) or via post on subsequent calls.
The problem was that I was going into the Firefox address bar and pressing Enter. This caused the URL to reload (and of course it didn't have the querystring after it reposted). So -- lesson is to do a check of the incoming vars and form vars to see if the page has been manually refreshed I suppose...
You could still maintain the submitted values in this situation.
What you would need to do is log the most recent request in either a Cookie, Session or data/file store, and on each request, check to see if the request was handled before you remove the data.
Since what you were after was the querystring it could just be something like this:
Response.Cookies("tempdata")("querystring") = Request.ServerVariables("QUERY_STRING")
Response.Cookies("tempdata")("querystring_handled") = false
then when you are done with that request you can clear the cookie value or set the querystring_handled = true.
There are probably situations where this could cause some conflicts, but just so you know, it is still going to be possible for you to remember the request once it is received by the server.
Which action does the form use: GET or POST? Normally, a form would use the POST action, but in this case, if you refresh the page with the posted form, you will not get anything in query string, because query string only gets passed via the GET action. Assuming that this issue is not caused by page caching, it seems to me like it works as designed (if the form POSTs data). Just make sure that you process the form variables if the query string is missing.

MVC 2.0 Post Form to action instead of redirect to action

I am using T4MVC to redirect to another action return RedirectToAction(MVC.MyController.MyAction());.
In result it is doing get request.
Is there any way to make post request from controller. I want to keep all the same but only make post instead get. I cant find any methods for that. I found one post helper here http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx but i cant pass any values i need using this post helper. I was trying to pass values through TempData but they are not coming when i using this helper. May be some one have any ideas?
The reason i want to do this because when user come from one controller to another and then if user click update or just click enter in browser address bar, page will break.
Should i use session for that reason?
A RedirectToAction will always perform a GET, never a POST (it returns a HTTP 302 to the browser, which will then issue a GET request).
To persist data across the redirect, if it is data that can be easily represented as a string and stored in the query string, then you can just add it to the route values of the redirect.
e.g.
return RedirectToAction("Search", new { searchString = "whatever" });
If it is a complex type, then you will need to store it in TempData. A number of other questions on StackOverflow (such as this one) give details on how.
If repeatedly storing to and reading from TempData across your application offends your code-sense, then you can encapsulate this by using the PassParametersDuringRedirect attribute and generic RedirectToAction available in the MvcContrib project. Some details on this technique are available here.
only way of doing post is by having a form and doing submit on that form, either with a submit button or with javascript, any info you want passed to that action must be in that form and you will find everything posted in FormCollection(hope I spelled it right).

What is the difference between redirect and forward in Symfony?

I want to know the difference between :
$this->forward("module", "action");
And
$this->redirect("module/action");
My first guess is that one implies a new request while the other one not, but I'm not sure.
In some cases, the action execution ends by requesting a new action execution. For instance, an action handling a form submission in a POST request usually redirects to another action after updating the database. Another example is an action alias: the index action is often a way to display a list, and actually forwards to a list action.
The action class provides two methods to execute another action:
If the action forwards the call to another action:
$this->forward('otherModule', 'index');
If the action results in a web redirection:
$this->redirect('otherModule/index');
$this->redirect('http://www.google.com/');
The choice between a redirect or a forward is sometimes tricky. To choose the best solution, keep in mind that a forward is internal to the application and transparent to the user. As far as the user is concerned, the displayed URL is the same as the one requested. In contrast, a redirect is a message to the user's browser, involving a new request from it and a change in the final resulting URL.
If the action is called from a submitted form with method="post", you should always do a redirect. The main advantage is that if the user refreshes the resulting page, the form will not be submitted again; in addition, the back button works as expected by displaying the form and not an alert asking the user if he wants to resubmit a POST request.