by Dinesh Thakur Category: Servlet Chaining

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.

                    How to use sendRedirect method

The HTML output of Srvl (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: http://localhost:2020/WA2/ s2url (Request URL of Srv2 program.)

                  How to use sendRedirect method

                  How to use sendRedirect method

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 Srvl program.

• All statements of Srvl servlet program execute, including the

 res.sendRedirect () method.

• Srvl 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 Srvl program goes to browser window as final response.

Key points:

Srvl 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 Srvl program Srv2 program append query String to the URL of rs.sendRedirect () method in Srvl 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 (“http://localhost:2020/WA2/s2url? p1=val1 & p2=val2”);

(or)

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

In Srv2 prog

String sl=req.getParameter (“p1”);

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

Srvl 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 Srvl 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 Srvl pro,>ram the entire HTML output of Srvl 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.

                    How to use sendRedirect method

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>

Srvl.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> Srvl: sum is : </b>" +sum);

         //redirect the request to Srv2 program of TestApp2

         System.out.println ("Srv2 : before res.sendRedirect (-)");

         res.sendRedirect("http://localhost:2020/TestApp2/s2url? P1="+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> Srvl </servlet-class>

</servlet>

<servlet-mapping>

<servlet-name> a </servlet-name>

<url-pattern> /Srvl </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 





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.