Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Hidden parameters using response.sendRedirect()

How would I pass hidden parameters? I want to call a page (test.jsp) but also pass 2 hidden parameters like a post.

response.sendRedirect("/content/test.jsp"); 
like image 634
Ashish Anand Avatar asked Jun 08 '13 15:06

Ashish Anand


People also ask

What is the use of sendRedirect () method?

sendRedirect() method redirects the response to another resource, inside or outside the server. It makes the client/browser to create a new request to get to the resource. It sends a temporary redirect response to the client using the specified redirect location URL.

What is the difference between response sendRedirect () and forward ()?

SendRediret() When we use the forward method request is transferred to other resources within the same server for further processing. In case of sendRedirect request is transferred to another resource to a different domain or different server for further processing.

What is difference between ServletResponse sendRedirect () and RequestDispatcher forward () method?

This is also called server side redirect. A RequestDispatcher forward() is used to forward the same request to another resource whereas ServletResponse sendRedirect() is a two step process. In sendRedirect(), web application returns the response to client with status code 302 (redirect) with URL to send the request.


2 Answers

TheNewIdiot's answer successfully explains the problem and the reason why you can't send attributes in request through a redirect. Possible solutions:

  1. Using forwarding. This will enable that request attributes could be passed to the view and you can use them in form of ServletRequest#getAttribute or by using Expression Language and JSTL. Short example (reusing TheNewIdiot's answer] code).

    Controller (your servlet)

    request.setAttribute("message", "Hello world"); RequestDispatcher dispatcher = servletContext().getRequestDispatcher(url); dispatcher.forward(request, response); 

    View (your JSP)

    Using scriptlets:

    <%     out.println(request.getAttribute("message")); %> 

    This is just for information purposes. Scriptlets usage must be avoided: How to avoid Java code in JSP files?. Below there is the example using EL and JSTL.

    <c:out value="${message}" /> 
  2. If you can't use forwarding (because you don't like it or you don't feel it that way or because you must use a redirect) then an option would be saving a message as a session attribute, then redirect to your view, recover the session attribute in your view and remove it from session. Remember to always have your user session with only relevant data. Code example

    Controller

    //if request is not from HttpServletRequest, you should do a typecast before HttpSession session = request.getSession(false); //save message in session session.setAttribute("helloWorld", "Hello world"); response.sendRedirect("/content/test.jsp"); 

    View

    Again, showing this using scriptlets and then EL + JSTL:

    <%     out.println(session.getAttribute("message"));     session.removeAttribute("message"); %>  <c:out value="${sessionScope.message}" /> <c:remove var="message" scope="session" /> 
like image 174
Luiggi Mendoza Avatar answered Sep 24 '22 16:09

Luiggi Mendoza


Generally, you cannot send a POST request using sendRedirect() method. You can use RequestDispatcher to forward() requests with parameters within the same web application, same context.

RequestDispatcher dispatcher = servletContext().getRequestDispatcher("test.jsp"); dispatcher.forward(request, response); 

The HTTP spec states that all redirects must be in the form of a GET (or HEAD). You can consider encrypting your query string parameters if security is an issue. Another way is you can POST to the target by having a hidden form with method POST and submitting it with javascript when the page is loaded.

like image 21
AllTooSir Avatar answered Sep 23 '22 16:09

AllTooSir