Servlet RequestDispatcher forward example

Servlet RequestDispatcher forward example

Posted in : Java Posted on : November 18, 2011 at 7:13 PM Comments : [ 0 ]

In this tutorial you will learn how to use forward() method of RequestDispatcher in Servlet

Servlet RequestDispatcher forward example

In this tutorial you will learn how to use forward() method of RequestDispatcher in Servlet

forward() method of RequestDispatcher forwards the request made by the client by the the resource (any of them servlet , jsp, html, etc.) on the server without including the content of the requested resource.

syntax :

void forward(request, response) throws ServletException, IOException

parameters :

request & response : These parameters must be the same as the responding servlet service method object's are passed.

Example :

In this example first I created a servlet class named which extends the HttpServlet class to support the http functionality to servlet, into which I defined the doGet() method into which the object's of HttpServeltRequest and HttpServletResponse are created. In the further steps I am forwarding the request to a responding servlet. And because I am forwarding the request when name and password will be matched successfully that is stored in the database table, so I am required to connect my servlet class to a database system (here I am using MySql) that's why I am doing the connectivity process in my servlet code also. I also created a html page that will be the first view page for the user from where name and password will be filled by them, after filling the info request will be send to the server and server will match the name and password if matched then it will generate the response for the client this response is saved by the ServletContext and the reference of ServletContext is taken by the getServletContext() method, this reference is used for getting the object of RequestDispatcher using the getRequestDispatcher() method of ServletContext. Parameter of this method specifies the calling resource. When the calling resource is called then the content of requested resource output is flushed out.


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
<form method="get" action="ServletRequestDispatcherForward">
<p>Enter your info in the following text boxes</p>
Enter your name <input type="text" name="text1"/><br>
Enter your password <input type="password" name="text2"/><br>
<input type="submit" value="submit"/>

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;

import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.reflect.ReflectionFactory.GetReflectionFactoryAction;

public class ServletRequestDispatcherForward extends HttpServlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
PrintWriter out= response.getWriter();

String className= "com.mysql.jdbc.Driver";
String url= "jdbc:mysql://";
String user= "root";
String password= "root";

String name= request.getParameter("text1");
String psw= request.getParameter("text2");

Connection con;
PreparedStatement ps;
ResultSet rs;

con= DriverManager.getConnection(url, user, password);

String sql= "SELECT name FROM UserLogin WHERE name = '"+name+"' AND password = '"+psw+"'";

rs= ps.executeQuery();
String n= rs.getString("name");
ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/LoggedIn");
rd.forward(request, response);
out.println("wrong name or password");


catch(SQLException sx)
catch(ClassNotFoundException cx)


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation="">



Output :

When you will execute the login.html page and fill the information then the page will be as :

when you will click on submit button and the field values are matched to the database table then output will be as :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:

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

Tutorial Topics