Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get HTTP post parameter in JSP

Tags:

jsp

http-post

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?

like image 919
Ozgur Dogus Avatar asked Mar 22 '13 12:03

Ozgur Dogus


People also ask

How to use get method in JSP?

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.


3 Answers

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.

like image 77
Subir Kumar Sao Avatar answered Oct 14 '22 00:10

Subir Kumar Sao


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.

like image 40
chintan Avatar answered Oct 13 '22 23:10

chintan


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.

like image 38
BalusC Avatar answered Oct 14 '22 00:10

BalusC