I am new to JSP. I have a jsp page where a parameter is passed to this jsp page with http post. I can see the parameter in firebug as you can see in the picture.
But in my page when I try to print the token variable the variable is always null. I print the variable as follows:
<%
String token = request.getParameter("token");
%>
What am I doing wrong? How can I get the token parameter?
The GET method has size limitation: only 1024 characters can be in a request string. This information is passed using QUERY_STRING header and will be accessible through QUERY_STRING environment variable which can be handled using getQueryString() and getParameter() methods of request object.
The token
attribute looks like a nonce
to me and some security filter might be removing the value from the request object.
What you have done to print the value is absolutely correct. I am not going for best practices but it should work.
Check your code for security filter and see if you can find out where the value is removed/overridden.
After seeing your web.xml.
The value is passed to the domain using POST. The request is internally redirected to welcome page and the value is lost. If you pass the value using GET the value will be retained.
You have two options:
I have tested both and it works just fine.
Here are some checks.
1) Form with method="post" in the JSP page.
<form name="form1" action="nextpage" method="post">
2) Name of the control on the JSP is the token. like.
<input type="text" name="token" value="chintan"/>
3) You get the submit button instead of the button means.
<input type="submit" name="submit" value="submit"/>
After checking all these points. It will give you exact error. And you will be able to fix it easily.
As per the screenshot, the POST request resulted in a HTTP 302 response, which means that it's been redirected. A redirect basically instructs the client to fire a brand new HTTP request on the given URL. This does not somehow magically take over all request parameters from a previous request.
You need to pass the request parameters which you'd like to retain in the redirected request along with the redirect URL yourself:
String token = request.getParameter("token");
response.sendRedirect("/CR?token=" + URLEncoder.encode(token, "UTF-8"));
An alternative is to not perform a redirect at all, but just a forward. Whether that's the right approach depends on the concrete functional requirement which is nowhere elaborated in the question. To learn about the difference, carefully read RequestDispatcher.forward() vs HttpServletResponse.sendRedirect() and of course also our servlets wiki page to learn how to properly use servlets for this kind of requirements. A JSP page is simply the wrong tool for the job.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With