8

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. enter image description here

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ozgur Dogus
  • 911
  • 3
  • 14
  • 38

7 Answers7

7

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:

  1. Create a direct url and pass the value to it using the post as you are doing. Eg: url - yourdomain.com/welcome.jsp.
  2. Ask the other project to pass the parameter in the url (GET request).

I have tested both and it works just fine.

Rudy Vissers
  • 5,267
  • 4
  • 35
  • 37
Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
  • Hi, this was one thing i didnt check. thanks for your answer at first. i just checked my web.xml file but I couldnt see any filter on my web.xml file. is there anything i can check for any other filters ? any other help will be great as well. – Ozgur Dogus Mar 25 '13 at 10:04
  • 1
    You need to provide more information on what frameworks you are using. Your web.xml should provide some hint. If it's OK then you may post it. – Subir Kumar Sao Mar 25 '13 at 10:06
  • can you give me your email adress . i prefer to post it on your email rather than public due to some security issues. – Ozgur Dogus Mar 25 '13 at 10:10
  • Currently after request is redirected to welcome.jsp i can still see the posted token variable in firebug. Do you stil think my problem is due to redirection ? if yes ,the first option is more feasible for me. But how can i redirect the post ? thanks – Ozgur Dogus Mar 25 '13 at 11:23
3

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.

chintan
  • 471
  • 3
  • 16
2

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
2

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>
jiacheo
  • 308
  • 1
  • 2
  • 12
1

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);
    }
}
%>
WPrecht
  • 1,340
  • 1
  • 17
  • 29
  • Does parameters.get() different from request.getParameter ? – Ozgur Dogus Mar 22 '13 at 13:20
  • `parameters.get()` is pulling the value out of the Map parameters, `request.getParameter` pulls it directly from the request object. The advantage of the Map is if you didn't get the name exactly right, you can iterate over the Map and see what is getting passed to the JSP instead of what you think ought to be getting passed. – WPrecht Mar 22 '13 at 13:35
  • can i get the parameter by just parameters.get("token") ? there will be only one parameter passed to my page. Thanks. – Ozgur Dogus Mar 22 '13 at 13:38
  • Yes, you can. But that's the exact same thing as saying `request.getParameter("token")`, which you say fails. So...either something else is going wrong, or the parameter name is not exactly "token". – WPrecht Mar 22 '13 at 13:51
  • You're not answering the question. – BalusC Mar 25 '13 at 13:15
  • 2
    I did answer the question as it was originally posed. The screenshot and additional information came later (and completely changes the problem). – WPrecht Mar 25 '13 at 13:51
1

You can use ${param.token} to print request parameters in jsp page

thoitbk
  • 269
  • 2
  • 9
  • 21
0

you can print like

${param.token}

This will print the value

PSR
  • 39,804
  • 41
  • 111
  • 151