1

by using the method forward() I can "send" variables using request.setAttribute().

What if i should use sendRedirect() instead?? How can i "send" a variable to the destination page?

I don't like to do something like this: sendRedirect("page?varName=varValues"), i don't like to see variables names and values written on the browser.

Is there a way to send variables in a hidden way when using sendRedirect()?

Thank you

MDP
  • 4,177
  • 21
  • 63
  • 119
  • 3
    you might want to take time to read [this](http://stackoverflow.com/questions/17001185/pass-hidden-parameters-using-response-sendredirect) – Ker p pag Aug 08 '14 at 08:34

3 Answers3

1

In theory, we cannot send the POST request by using the sendRedirect().

But there is a possible solution if you insiste on passing the hidden parameters by sendRedirect(). And you can set those parameters in the object session.

For example :

HttpSession session = request.getSession(false);

session.setAttribute("parameter", "parameter");

response.sendRedirect("/page");
lxu
  • 21
  • 2
0

You could set the Attribute on the session and not the request so you can access it from the redirected page. But there is multiple solutions.

Kabulan0lak
  • 2,116
  • 1
  • 19
  • 34
0

The possible way is is to set in the session like something below,

session.setAttribute("varName", "value");
response.sendRedirect("/resultPage");

This will redirect to the result page and you can get the value from session as,

session.getAttribute("varName");

the session object will be available through out the application till you invalidate it manually.

The reason why RequestDispatcher forward() method holds the request object is that it forwards the same request and sendRedirect() doesnt do that because it creates new request each time

Learn More ..

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56