• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Servlet » Chaining » How to use sendRedirect method
Next →
← Prev

How to use sendRedirect method

By Dinesh Thakur

This method sends a temporary redirect response to the client using the mentioned redirect location URL. This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading ‘/’ the container interprets it as relative to the current request URL. If the location is relative with a leading ‘/’ the container interprets it as relative to the servlet container root.

If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written.

Parameters

loc – the redirect location URL

Throws

IOException-If an input or output exception occurs

IllegalStateException – If the response was committed or if a partial URL is given and cannot be converted into a valid URL.

The HTML output of Srv1 (SRC) program will be discarded and only the HTML output of Srv2 (dest) servlet program will be displayed on the browser window as response.

/s2url is the URL pattern of Srv2 servlet program.

URL: https://ecomputernotes.com:2020/WA2/ s2url (Request URL of Srv2 program.)

                  

                  

Using sendRedirection concept we cannot perform response inclusion but we can forward request from the servlet program to destination web resource program.

With respect to the diagrams:

• Browser window gives initial request to Srv1 program.

• All statements of Srv1 servlet program execute, including the

 res.sendRedirect () method.

• Srv1 program generates implicit response to browser window having the URL placed in sendRedirect () method as argument value. The response status code of the implicit response is 300-399(indicates redirection).

• Browser window uses the URL coming from implicit response (because of 300-399 status code) and generates implicit request to Srv2 program.

• All the statements of Srv2 program executes.

• The output of Srv1 program goes to browser window as final response.

Key points:

Srv1 and Srv2 will not use some request and response objects. They use different sets of req, res objects. So, the request to Srv2 program is not visible and accessible in Srv2 program. To send additional data from Srv1 program Srv2 program append query String to the URL of rs.sendRedirect () method in Srv1 program and read those values in Srv2 program as request parameter values.

In Srv1 prog

res.sendRedirect (“/s2url? pl=vall & p2=va12”);

 

(or)

res.sendRedirect (“https://ecomputernotes.com:2020/WA2/s2url? p1=val1 & p2=val2”);

 

(or)

res.sendRedirect (“http://machine2:7001/WA2/s2url?p1=val1&p2=val2”);

 

In Srv2 prog

String s1=req.getParameter (“p1”);
String s2=req.getParameter (“p2”); 

Srv1 and Srv2 can be there in the same web application or can be there in two different web applications of the same server or two different servers (these two servers can be there in the same machine or in two different machines).

If Srv1 and Srv2 reside in the same web application, we can pass relative path in sendRedirect () method otherwise we must pass absolute URL.

Srv2 can be a servlet program or JSP program or HTML program or ASP program or ASP.NET program or PHP program, etc.

The moment res.sendRedirect () method executes in Srv1 pro,>ram the entire HTML output of Srv1 program will be discarded.

To pass the request of one website to another without worrying about their technology environment, we can use sendRedirection.

Example: IBM has acquired national.com so the request given to nalonal.com will be redirected to certain web resource program of ibm.com.

Example application on send redirection

Note: In the application below, Srv1 sends three values to Srv2 program by

appending query String to the URL of response.sendRedirection () method. These values are form data and sum result.

Step 1: Prepare the deployment directory structure of web application.

                    

Step 2: Develop the source code of above servlet program or web application.

Test Appl (web application)

input.html

<form action= “slurl”>
 A value : <input type= “text” name= “tl”> <br>
 B value : <input type= “text” name= “t2”< <br>
<input type= “submit” value: =“getResult”> </form>

Srv1.java

import javax.servlet.*;
import javax.http.*;
import javax.io.*;
public class Srv1 extends HttpServlet {
 public void service (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
 //general settings
 PrintWriter pw=res.getWriter ();
 setContentType ("text/html");
 //read form data
 int val1 =Integer.parseInt (req.getParameter ("t1"));
 int val2 =Integer.parseInt (req.getParameter ("t2"));
 //find out sum value
 int sum=val1+val2;
 pw.println ("<br> Srv1: sum is : </b>" +sum);
 //redirect the request to Srv2 program of TestApp2
 System.out.println ("Srv2 : before res.sendRedirect (-)");
 res.sendRedirect("https://ecomputernotes.com:2020/TestApp2/s2urlP1="+val1+"&p2= 
 "+val2+"&p3="+sum);
 System.out.println ("Srv2 : after res.sendRedirect (-)");
 //close stream obj
 pw.close() ;
 }
}

web.xml (For Tomcat server)

web.xml

<web-app>
 <servlet>
   <servlet-name> a </servlet-name>
   <servlet-class> Srv1 </servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name> a </servlet-name>
  <url-pattern> /Srv1 </url-pattern>
 </servlet-mapping>
</web-app>

web.xml (For Tomcat Server)

<web-app>
 <servlet>
   <servlet-name> b </servlet-name>
   <servlet-class> Srv2 </servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name> b </servlet-name>
  <url-pattern> /Srv2 </url-pattern>
 </servlet-mapping>
</web-app>

Source Code(TestApp2)

Srv2.java

import javax.servlet.*;
import javax.http.*;
import javax.io.*;
public class Srv1 extends HttpServlet {
 public void service (HttpServletRequest req, HttpServletResponse res) 
  throws ServletException, IOException {
 //general settings
 PrintWriter pw=res.getWriter ();
 res.setContentType ("text/html") ;
 //read form data
 int val1=Integer.parseInt (req.getParameter ("p1"));
 int val2=Integer.parseInt (req.getParameter ("p2"));
 int sum=Integer.parseInt (req.getParameter ("p3"));
 //find out product
 int mul=val1*val2;
 //display results
 pw.println ("Srv2: sum is ;"+sum);
 pw.println ("Srv2: product is :"+mul);
 System.out.println ("from srv2 program");
 pw.close () ;
 }
}

Step 3: Compile the source files of all servlet programs.

Step 4: Configure all the four servlet programs in web.xml file having four different url patterns.

Step 5: Start the server (Tomcat)

Step 6: Deploy the web application.

Copy TestAppl folder to Tomcat_home\webapps folder

Copy TestApp2 folder to Tomcat_home\webapps folder

Step 7: Test the web application.

Open browser window type these urls: http:/ / localhost:2020/TestApp1/ form.html 

You’ll also like:

  1. Difference Between Rd.Forward () Res. sendRedirect ()
  2. How to use RequestDispatcher include method
  3. How to use RequestDispatcher Forward method
  4. Seek method
  5. Type of Method in C#
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Servlet Tutorials

Servlet Tutorials

  • Servlet - Home
  • Servlet - Types
  • Servlet - Advantages
  • Servlet - Container
  • Servlet - API
  • Servlet - Chaining
  • Servlet - Life Cycle
  • Servlet - Developement Way
  • Servlet - Servlet Vs CGI
  • Servlet - Server Side
  • Servlet - HttpServletRequest
  • Servlet - Cookies Advantages
  • Servlet - JDBC Architecture
  • Servlet - HttpServlet
  • Servlet - HttpServletResponse
  • Servlet - Web Technology
  • Servlet - Applet Communication
  • Servlet - Html Communication
  • Servlet - HTTP POST Request
  • Servlet - HTTP GET Request

Other Links

  • Servlet - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW