ATG - session or request attribute coming as null when checkFormRedirect is called - atg

In my ATG application, when I am redirecting user to jsp page with some parameters using checkFormRedirect, I am getting parameters as null. Please see below FormHandler code:
UserFormHandler:
public boolean handleUserRedirect(dynamo req, dynamo res){
//using request
req.setParameter("test", "testdata");
//using session
HttpSession session=req.getSession();
session.setAttribute("uname","testdata");
//redirect to test.jsp
return checkFormRedirect("/test/test.jsp","null",req,res);
}
test.jsp :
<% out.println(session.getAttribute("uname")); %>
<% String stErrorMsg=(String)session.getAttribute("uname");%>
<%=stErrorMsg %>
<% request.getParameter("test")%>
Also, I have tried using variable in my formHandler and setting value and still I am getting value as null. Can some help on this.

Generally, you cannot send a POST request using sendRedirect() method. You can use RequestDispatcher to forward() requests with parameters within the same web application, same context.
RequestDispatcher dispatcher = servletContext().getRequestDispatcher("test.jsp");
dispatcher.forward(request, response);
The HTTP spec states that all redirects must be in the form of a GET (or HEAD). You can consider encrypting your query string parameters if security is an issue. Another way is you can POST to the target by having a hidden form with method POST and submitting it with javascript when the page is loaded.
So you can use Session approach: I tried out I get the value in JSP.
<%
out.println(session.getAttribute("message"));
session.removeAttribute("message");
%>
/* Or using JSTL */
<c:out value="${sessionScope.message}" />
<c:remove var="message" scope="session" />
screenshot
Hope this help.

Related

Is there a way to prevent csrf popup form in playframework 2.6

I work with playFramework 2.6. I have a popup that contains a form. When I try to submit the form, I get the following error:
Erreur Client!403 - No CSRF token found in body.
How can I fix this error?
Enable CSRF folder within application.conf:
play.filters.enabled += "play.filters.csrf.CSRFFilter"
Then in your form have this:
<form action="/submit-form-url" method="POST">
#CSRF.formField
//Your form body
</form>
In case you dont want to use the helper (#CSRF.formField)
First import the required library: import play.filters.csrf.CSRF.
Second, get its value within the controller method: CSRF.getToken and pass it on the views.
Third, within the views (after the form initial line) use the hidden input to hold the token value:
<input type="hidden" name="csrfToken" value="whatever-that-is">

Consume RESTful Service from form post

I've built a RESTful service with Spring (java annotation based configuration) which I can execute successfully via Curl. I'm trying to submit files via a HTML Form too, however that's not working.
#RequestMapping(path = "/upload", method = RequestMethod.POST)
public String handleFileUpload(#RequestPart(value = "file") MultipartFile file,
RedirectAttributes redirectAttributes) {
logger.info("POST '/upload'");
storageService.store(file);
redirectAttributes.addFlashAttribute("message",
"You successfully uploaded " + file.getOriginalFilename() + "!");
return "redirect:/";
}
And I'm submitting a file via Curl as follows:
curl -i -H "Content-Type: multipart/*; boundary=------------BOUNDARY--" -X POST --noproxy localhost, localhost:8080/upload -F "file=#test.txt"
Even though my form submission has an input of type file, with the name file, I get this error via a HTML form
org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present
From what I've been investigating, it would seem as if the controller would expect a Model Attribute when the file is submitted via HTML Form, so what could be a good practice to resolve this? Include a controller parameter for a Model Attribute, which would be checked for null value (to distinguish between html form or other submission methods), or would a sort of Proxy controller between the HTML Form and the RESTful service be better?
The service looks good. It works fine for me on both CURL and HTML form submit.
Here is my form. Please try using this.
<html>
<head>
<title>Post Tool</title>
</head>
<body>
<h1>Sample Requests</h1>
<h2>Upload Document</h2>
<form action="http://localhost:8080/upload" method="post" enctype="multipart/form-data">
Choose a file : <input type="file" name="file" multiple/>
<input type="submit" value="Upload" />
</form>
</body>
</html>
After more research I got to know that the commons multi part resolver doesn't fare well with the new versions of spring boot. It struggles obtaining POST parameters from forms.
One solution is to use the StandardServletMultipartResolver implementation of the MultipartResolver interface!

How to convert a html form into a url

I am creating buttons that send the customer to a hosted payment system. In this case an authorize.net Simple Checkout button in test mode.
<form name="PrePage" method = "post" action = "https://Simplecheckout.authorize.net/payment/CatalogPayment.aspx"> <input type = "hidden" name = "LinkId" value ="8a40541d-2f0f-4bfe-a1e8-397292f5dee5" /> <input type = "image" src ="//content.authorize.net/images/buy-now-gold.gif" /> </form>
My attempt to get the form inputs into the url are following:
https://Simplecheckout.authorize.net/payment/CatalogPayment.aspx/?LinkId=8a40541d%2D2f0f%2D4bfe%2Da1e8%2D3d397292f5dee5
What am I doing wrong?
The diffence is that you are using the GET method instead of POST.
When you include data in your query string (things after the ? in the URL) you are using the GET method.
If you have a form you can specify to use the POST (as it is in your code example also). In this set up the data is transferred a different way. Not in the query string.
see: HTTP - Post and Get
Maybe your server is not handling, only the POST method, so your GET request won't work.

Why do forms timeout in ColdFusion?

We're on CF 8 and we have a CF application with a 1 hour session timeout specified.
<cfapplication sessionmanagement="yes" sessiontimeout="#CreateTimeSpan(0, 0, 60, 0)#" name="myAppName" clientmanagement="yes">
Within the CF administrator, we have a default session timeout of 1 hour, and a max session timeout of 1 day.
We're having some odd (intermittent) form timeout issues on submission of a regular form (not a flash form)...
Let's say the user hits the form page at 10:30am. (I know this because I'm appending the time to the "Action" property of the form tag). At 11:10am, the user submits the form, but none of the form struct is available to the action page, so the user gets an error.
I know that it's coming from the correct page since the CGI.referrer is defined properly.
In my custom error handler, I iterate over any form, session, or client structs -- and there is NO form struct defined at this point. All of the other Session and Client variables ARE still available.
Excerpts from Form Page:
<cfform name="chairsComment" id="chairsComment" action="library/save_chairsComment.cfm?Start=0224_153027" method="post" >
<input name="chairsCommentNumber" id="chairsCommentNumber" type="hidden" value="13" />
<textarea name="comment_13" rows="50" wrap="virtual" cols="100" id="comment_13" ></textarea>
<input name="save_answer" id="save_answer" type="submit" value="Save Response" />
</cfform>
And for the Action page, it throws an error on line 1:
<cfset whichCommentNumber = form.chairsCommentNumber>
It works during testing, but when some of our users use it, it throws the error that "Element CHAIRSCOMMENTNUMBER is undefined in FORM." We've just started looking at the specific browser that this individual is using (Safari 4.0.3) and asked him to upgrade to 5.x, but with SUCH a vanilla form, it seems an unlikely culprit.
Any Ideas?
In the midst of a discussion on Ray Camden's blog about file uploading, someone mentions a problem with Safari 4.0.5 throwing an error because the form field did not contain a file ... it's not the same problem, necessarily, but it could be related. Unfortunately, the commenter never returned with more information.
There's a post on another blog here where a commenter mentions an issue with Safari and a cfform inside a cfdiv tag. If your cfform is similarly nested, you might need to reverse the order (nest the cfdiv inside the form) to make this work.

How to page while maintaining the querystring values in ASP.Net Mvc 2

I am using the pager provided by Martijn Boland to implementing paging in my Asp.Net Mvc 2 application.
My form uses the GET method to send all parameters to the querystring, it is a search form with several form elements.
<% using (Html.BeginForm("SearchResults", "Search", FormMethod.Get))
{%>
On the SearchResults View I am trying to implement paging:
<div class="pager">
<%= Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount,
new { Request.QueryString })%>
</div>
The Html.Pager has some overloads which I am not too clear on how to use. The Request.QueryString makes the querystring look like this:
http://localhost:1155/Search/SearchResults?QueryString=Distance%3D10%26txtZip%3D%26cb&page=2
Should it not be like this?
http://localhost:1155/Search/SearchResults?Distance=20&txtZip=10021&page=2
my guess would be to write your pager like this
<%= Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount, new { Distance = Request["Distance"], txtZip = Request["txtZip"] })%>
but it's only a guess, i've never used that...
Edit: see ASP.Net MVC Keeping action parameters between postbacks
so you have to create a RouteValueDictionary from the QueryString which is a NameValueCollection.