Pass Control from one JSP page to another

Write Sample Code to pass control from one JSP page to another?

Questions by Aparna Rao

Showing Answers 1 - 4 of 4 Answers

There are 2 ways to redirect the page
1) Use the forward :- By using this
you will get the all the objects in the redirected page.
2) response.sendRedirect("Path") :- In This case the control is not transferred to the next page.

delindia

  • Jan 2nd, 2012
 

The RequestDispatcher objects forward method to pass the control.
The response.sendRedirect method

Code
  1. <%      response.sendRedirect("RedirectIfSuccessful.html");            %>

  2.  

  3. <%

  4. String st=request.getParameter("n");

  5. if(st==""){

  6. getServletContext().getRequestDispatcher("/form.jsp").forward(request, response);

  7. }

  8. else{

  9. getServletContext().getRequestDispatcher("/welcome.jsp").forward(request, response);

  10. }

  11. %>

  12.  

  Was this answer useful?  Yes

raman

  • Nov 12th, 2014
 

Hi,
you want to call it inside of server ?

then save your values to session and do include ( if you want to continue on top page ) or (redirection) forward - if you want to finish on called page

example 1. include :

Code
  1. String obj = "test";

  2.    session.setAttribute("some_name_here",obj);

  3.    pageContext.include("relative name of your page");

  4.    ...

  5.    ...

  6.  

  7.    in called page you do:

  8.  

  9.    String obj = (String) session.getAttribute("some_name_here");

  10.     .... use variable



example 2. forward :
same like previous but replace pageContext.include by :

<%@ forward file="your page name" %>


for security issues is sometimes better to use ( if its possible )

response.sendRedirect(" some page ");
out.flush();
return;

but take care of output buffer ( to not get exception )

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions