CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > What is > Servlet Redirections

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Servlet Redirections

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 5 min read ยท 2 Comments
Share: in X
git change remote url

Whether building a new Web application or adding some new pages in the existing Web application, it is always required to forward the processing of a request to another Servlet, or to include the output of another servlet in the response. This can be achieved by two ways of servlet redirections.

Following are the methods of Servlet redirections:

  • sendRedirect method from HttpServletResponse and
  • RequestDispatcher interface.

sendRedirect method

This method redirects the response back to the client’s browser with the status code. The container decides whether it can handle the request or not. If the container identifies that the request can’t be handled then the browser will normally interpret this response by initiating a new request to the redirect URL given in the response. This method works only in the browser.

Example:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class RedirectDemo extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String un = request.getParameter("username");

        if (un.equals("coder")) {
            response.sendRedirect("SuccessServlet");
        } else {
            response.sendRedirect("FailedServlet");
        }
    }
    
}

Because the browser issues a completely new request, any objects stored as request attributes before the redirect occurs will be lost. Hence, the requested data is lost.

RequestDispatcher interface

The RequestDispatcher allows the dynamic inclusion of web components either by including in the current component or by forwarding to another web component. It takes requests from the client and sends them to other resources available on the server. Resources could be any Servlet, HTML, JSP, or other. A RequestDispatcher has the following two primary methods.

  • Include: This is for including the response of another program in the currrent request.
  • Forward: This is forwarding the request of the current program to another one.

An object implementing the RequestDispatcher interface may be obtained via the following methods:

  • ServletContext.getRequetDispatcher(String path)
  • ServletRequest.getRequestDispatcher(String path) and
  • ServletContext.getNamedDispatcher(String path)

RequestDispatcher with ServletContext

A RequestDispatcher object can be obtained by calling the getRequestDispatcher method of the ServletContext object. An instance of ServletContext can be obtained by calling the getServletContext method of the HttpServlet class. Following are the steps of getting an instance of RequestDispatcher.

  1. Get a servlet context instance from the servlet instace

ServletContext sc = this.getServletContext();

  1. Get a request dispatcher from the servlet context instance specifying the page-relative or application-relative path of the target JSP or Servlet as input to the getRequestDispatcher() method.

RequestDispatcher rd = sc.getRequestDispatcher("/SuccessServlet");

  1. Invoke the include() or forward() method of the request dispatcher specifying the HTTP request and response objects as parameters.

rd.forward(request, response);

or

rd.include(request, response);

Example:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class RedirectDemo extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String un = request.getParameter("username");

        ServletContext sc = this.getServletContext();
        
        if (un.equals("coder")) {
            RequestDispatcher rd = sc.getRequestDispatcher("/SuccessServlet");
            rd.forward(request, response);
        } else {
            response.sendRedirect("FailedServlet");
        }
    }
    
}

ServletContext method requires an absolute URL that must begin with / (Forward Slash). If you try to use the URL without / then it will show an exception.

Let’s see an example:

ServletContext sc = this.getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher("SuccessServlet");
rd.forward(request, response);

At the runtime, this code will throw an exception:

HTTP Status 500 โ€“ Internal Server Error
Type Exception Report

Message Path [SuccessServlet] does not start with a "/" character

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

java.lang.IllegalArgumentException: Path [SuccessServlet] does not start with a "/" character
	com.codersathi.servlet.RedirectDemo.doGet(RedirectDemo.java:23)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.

RequestDispatcher with ServletRequest

The ServletRequest.getRequestDispatcher method allows relative paths that are relative to the path of the current request (not relative to the root of the ServletContext). It is provided in the ServletRequest interface. The behavior of this method is similar to the method of the same name in the ServletContext.

Example:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class RedirectDemo extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String un = request.getParameter("username");
       
        if (un.equals("coder")) {
            RequestDispatcher rd = request.getRequestDispatcher("SuccessServlet");
            rd.forward(request, response);
        } else {
            response.sendRedirect("./FailedServlet");
        }
    }
    
}

In the above example, we haven’t started a URL Pattern value with / (Forward Slash). Because it does not make mandatory to add it, unlike in the ServletContext. Hence, it still works even we add /.

ServletContext.getNamedDispatcher(String servletName)

The ServletContext.getNamedDispatcher method takes a string argument indicating the Name of the servlet known to the ServletContext. If a servlet is found, it is wrapped with a RequestDispatcher object and the object is returned. If not servlet is found with the given name then the method will return null.

When RequestDispatcher object is null then it will throw NullPointerException. The detailed message is:

java.lang.NullPointerException: Cannot invoke "javax.servlet.RequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)" because "rd" is null

Example:

Let’s say we have defined a servlet on the web.xml file shown below.

<servlet>
      <servlet-name>redirectDemo</servlet-name>
      <servlet-class>com.codersathi.servlet.RedirectDemo</servlet-class>
 </servlet>

Now we can give the Servlet name in ServletContext.getNamedDispatcher(String servletName):

RequestDispatcher rd = sc.getNamedDispatcher("RedirectDemo"); // Here the RedirectDemo is the servlet name not URL Pattern.
rd.forward(request, response);

Conclusion

In this post, we learned the various ways of Servlet Redirection in detail. In RequestDispatcher there are two methods forward and include. When to choose between them is discussed in another post Servlet Request Dispatcher method forward vs include

Related Posts:

  • Servlet RequestDispatcher forward vs include
  • A Simple Servlet Program in Java
  • Interface in Java: Mastering Abstraction and…
  • javax.servlet package
  • Cookie in Servlet
  • How To Read Servlet Parameters in Java: A Step-by-Step Guide
Tags:javaservlet
Was this article helpful?
โ† Previous ArticleConstructor in Java
Next Article โ†’HTTP Methods

2 thoughts on “Servlet Redirections”

  1. ื ืขืจื•ืช ืœื™ื•ื•ื™ ื‘ื—ื™ืคื”
    March 28, 2022 at 7:57 am

    This is the right website for anybody who wants to find out about this topic. You understand a whole lot its almost tough to argue with you (not that I personally would want toร–HaHa). You definitely put a new spin on a topic which has been written about for years. Excellent stuff, just excellent!

    Log in to Reply
    • Coder Sathi
      March 28, 2022 at 1:11 pm

      Thank you for the feedback. Keep visiting Codersathi. ๐Ÿ™‚

      Log in to Reply

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap