JSP Redirection from a Servlet.

Hello Friends,

This tutorial is regarding the jsp redirection from a Servlet to a another jsp page. Most of you may need to redirect the user to a jsp page being on the servlet page. This tutorial will help you. There are two ways to achieve this, we will discuss them both.

Firstly, we can see the simpler one:-


resp.sendRedirect(req.getContextPath()+ “/pages/redirect.jsp”);


Where, “/pages/redirect.jsp” is the location of the file under the Webcontent.

Or, there is another way for this using the:


String destination = “/pages/redirect.jsp”;
RequestDispatcher red = getServletContext().getRequestDispatcher(destination);
red.forward(request, response);


Here, request is the instance of HttpServletRequest class and response is the instance of HttpServletResponse class. RequestDispatcher.forward() and PageContext.forward() are effectively the same. PageContext.forward is a helper method that calls the RequestDispatcher method. When you invoke RequestDispatcher.include(), the servlet engine transfers control of this HTTP request internally from your current servlet or JSP to another servlet or JSP or static file, while invoking response.sendRedirect() method sends an HTTP response to the browser to make another request at a different URL.

You can use any of the above for the page redirection.

Hope this will help you.


Nikhil Naoghare

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.