Forwarding Request From One Servlet to Other

Forwarding Request From One Servlet to Other


Posted in : Servlet Posted on : November 20, 2010 at 4:46 PM Comments : [ 0 ]

In this tutorial you will learn how to forward request using RequestDispatcher and sendRedirect from one servlet to other

Forwarding Request From One Servlet To Other

To forward request from one servlet to other either you can user RequestDispatcher or SendRedirect. To use RequestDispatcher you must have to get ServletContext reference and then you have to call the getRequestDispatcher() method of ServletContext and using SendRedirect you have to write response.sendRedirect("URL").  An example based on RequestDispatcher and SendRedirect is given below.

login.html


<HTML>
<HEAD>
<TITLE> Login Page </TITLE>
</HEAD>
<BODY>
<BODY bgcolor=lightblue>
<form name=login method="get" 
action="http://localhost:8080/forward/check">
<center>
<h1>Enter your Name and Pasword </h1><br>
<table border=1>
<tr><td>Enter Your Name :</td>
<td><input type="text" name="userName" value=""></td>
<tr><td align=center><input type="submit" name="Submit" value="Submit" ></td>
<td align=center><input type="reset" name="reset" value="Refresh"></td>
<table>
</center>
</form>
</BODY> 
</HTML>

CheckServlet.java

package roseindia.net;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CheckServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
String name = request.getParameter("userName");
if (name != null && name.equalsIgnoreCase("web")) {
out.println("<Center><h3>");
out.println("Forwarding Request.........");
out.println("</h3></Center>");
// forwarding using Request Dispatcher
ServletContext servletContext = getServletContext();
RequestDispatcher requestDispatcher = servletContext
.getRequestDispatcher("/home");
requestDispatcher.forward(request, response);
// Or you can use same thing as
// response.sendRedirect("/home");

} else {
ServletContext servletContext = getServletContext();
RequestDispatcher requestDispatcher = servletContext
.getRequestDispatcher("/error");
requestDispatcher.forward(request, response);
// response.sendRedirect("/error");
}
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}
}

HomeServlet.java

package roseindia.net;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HomeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("<Center><h3>");
out.println("Welcome To Home");
out.println("</h3></Center>");
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}
}

ErrorServlet.java

package roseindia.net;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ErrorServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("<Center><h3>");
out.println("Error in login.........");
out.println("</h3></Center>");
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="
http://java.sun.com/xml/ns/j2ee" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="
http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>CreateSession</display-name>
<!-- Servlet -->

<servlet>
<servlet-name>CheckServlet</servlet-name>
<servlet-class>roseindia.net.CheckServlet</servlet-class>
</servlet>

<servlet>
<servlet-name>HomeServlet</servlet-name>
<servlet-class>roseindia.net.HomeServlet</servlet-class>
</servlet>

<servlet>
<servlet-name>ErrorServlet</servlet-name>
<servlet-class>roseindia.net.ErrorServlet</servlet-class>
</servlet>

<!-- Servlet Mapping-->

<servlet-mapping>
<servlet-name>CheckServlet</servlet-name>
<url-pattern>/check</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>HomeServlet</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>ErrorServlet</servlet-name>
<url-pattern>/error</url-pattern>
</servlet-mapping>

<!-- Setting Session Time Out-->

</web-app>
When you run this application it will display message as shown below

 


Download this example code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics