Reputation: 921
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?
Upvotes: 8
Views: 92032
Reputation: 471
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.
Upvotes: 3
Reputation: 8411
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.
Upvotes: 7
Reputation: 308
as you saw in the image, the http response code is 302
That is why you could not get the token parameter, because when the browser get to your jsp page, it is from redirect, but not request forward.
That is say, your browse request your jsp page with not any parameters...
You can check if your posted action is a servlet and then redirect to the jsp page ????
check your action code and change the code
response.sendRedirect....
to
request.getRequestDispacher("your jsp page path").forword(request, response)
or just change your form submit straightly to the jsp page
<form action="your jsp page link">
...
</form>
Upvotes: 2
Reputation: 1109875
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.
Upvotes: 2
Reputation: 1382
The preferred idiom for this would be to use a servlet from the form, not a JSP. The servlet can then later use a JSP as a view. One of the reasons is to avoid doing what you appear to be doing: putting java code on the page and likely mixing presentation with logic.
However, if you had no choice I would recommend using JSTL:
<c:forEach var="par" items="${paramValues}">
<c:if test="${fn:startsWith(par.key, 'token')}">
${par.key} = ${par.value[0]};
</c:if>
</c:forEach>
Or in Java:
<%@ page import = "java.util.Map" %>
<%
Map<String, String[]> parameters = request.getParameterMap();
for(String parameter : parameters.keySet()) {
if(parameter.toLowerCase().startsWith("token")) {
String[] values = parameters.get(parameter);
}
}
%>
Upvotes: 1