How to redirect from Servlet to Jsp or Servlet to Servlet.JSP Navigation

How we can redirect from Servlet to Jsp or Servlet to Servlet :-

1) using response.sendRedirct()

     It will complete roundtrip from server to client then the client to the server means if we will redirect to the JSP page then the response will be submitted to the browser then the browser will navigate the content into different web pages.

2) using Request Dispatcher Interface using forward and include

  It will not complete a round trip means Request-dispatcher will directly send a request to another web page from a web server using forward and include method hence we can not manage session and cookie using Request-dispatcher object.

1)  forward():-   We can directly send the request from the servlet class to JSP web content from Web Server, the URL will not be changed because page redirection will be managed by Server.

2)  include():-   We can include the content of the web page to servlet class means it will display combined result means the result of servlet and JSP page both will be displayed using response object.

Code of Servlet:

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        RequestDispatcher req =  request.getRequestDispatcher("ArrExample.jsp");
        req.include(request, response);
        //req.forward(request, response);
       // response.sendRedirect("ArrExample.jsp");


3)  using JavaScript 

We will write 


window.location='jsppage or servlet url';

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post