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 ServletRequestDispatcherForward.java 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.

login.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<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"/>
</form>
</body>
</html>

ServletRequestDispatcherForward.java

import java.io.IOException;
import java.io.PrintWriter;
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
{
response.setContentType("text/html");
PrintWriter out= response.getWriter();

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

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

Connection con;
PreparedStatement ps;
ResultSet rs;

try
{
Class.forName(className);
con= DriverManager.getConnection(url, user, password);

String sql= "SELECT name FROM UserLogin WHERE name = '"+name+"' AND password = '"+psw+"'";
ps=con.prepareStatement(sql);

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

con.close();
ps.close();
rs.close();
}

catch(SQLException sx)
{
out.println(sx);
}
catch(ClassNotFoundException cx)
{
out.println(cx);
}
}
}

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>servletRequestDispatcher</display-name>
<servlet>
<servlet-name>ServletRequestDispatcherForward</servlet-name>
<servlet-class>ServletRequestDispatcherForward</servlet-class>
</servlet>
<servlet>
<servlet-name>LoggedIn</servlet-name>
<servlet-class>LoggedIn</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ServletRequestDispatcherForward</servlet-name>
<url-pattern>/ServletRequestDispatcherForward</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoggedIn</servlet-name>
<url-pattern>/LoggedIn</url-pattern>
</servlet-mapping>

</web-app>

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