Servlet RequestDispatcher include example

Servlet RequestDispatcher include example


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

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

Servlet RequestDispatcher include example

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

include() method of RequestDispatcher includes the content of the requested resource (any of them jsp, servlet, heml, etc) on the server response.

syntax :

void include(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 ServletRequestDispatcherInclude.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 sending the request to a calling resource. And because I am sending the request when name and password will not 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 user 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 and the output will be showed but if not matched then 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 will be included with the calling resource.

userLogin.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>User Login Page</title>
</head>
<body>
<form method="get" action="ServletRequestDispatcherInclude">
<p>Enter your info in the following text boxes</p>
User name <input type="text" name="text1"/><br><br>
Password <input type="password" name="text2"/><br>
<input type="submit" value="submit"/>
</form>
</body>
</html>

ServletRequestDispatcherInclude.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;

public class ServletRequestDispatcherInclude 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("Welcome "+n);
}
else
{
ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/userLogin.html");
out.println("<font color=red>either user name or password is incorrect</font>");
rd.include(request, response);
}

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>ServletRequestDispatcherInclude</servlet-name>
<servlet-class>ServletRequestDispatcherInclude</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ServletRequestDispatcherInclude</servlet-name>
<url-pattern>/ServletRequestDispatcherInclude</url-pattern>
</servlet-mapping>

</web-app>

Output :

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

when you will click on submit button and the field value is not 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