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:-

[java]

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

[/java]

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

Or, there is another way for this using the:

[java]

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

[/java]

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.

Regards,

Nikhil Naoghare

Related Posts

  • Abstract Class In JAVA

    Hello Friends, This tutorial is for all the Java followers. One of the best feature that is widely used is the term ‘Abstract’. This term can be used as either class or a simple method. An abstract method is any method that is just declared but not instantiated. In other words one can just create…

  • Threads in Java.

    Hello Friends, This is the tutorial for the java developers. One of the most significance feature of core java is Threading. Threading deals with the processing of Threads in a single java program. Let us learn what actually are Threads. *What are Threads? Threads are independently running processes that are isolated from each other upto…

  • Collections In Java.

    Hello friends, Welcome to another tutorial for java followers. You all may have heard about Collections, it is one of the amazing feature in java. Collections are the object for the group of elements, these elements are nothing but the different data structures like as Array Lists, Linked Lists, Vectors, Hash tables,Hash List, Trees, Hash…

  • Maven Installation.

    Hello Friends, This is one of my tutorial for installation of Maven on windows. Maven is a software project management tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information. Maven is an open source community and is a…

  • Jira Plugin Deployment.

    Hello Friends, This is one of the most important tutorial for all the jira developers. You may have heard about the plugins in jira which is a simple Java Archive File with .jar extension. This JAR file contains the class files and auxiliary resources associated with the applications. In Jira we can use Eclipse IDE…

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.