@WebServlet RequestDispatcher Include Example

In this tutorial you will learn how to include the content into another resources in the response.

@WebServlet RequestDispatcher Include Example

In this tutorial you will learn how to include the content into another resources in the response.

Sometimes it is required to include the another resource's content or want write to the body in the response that is returned from a web component. include(request, response) method of RequestDispatcher is used for including the another resource's content/information. The example is given below will demonstrate you to how can you include the content/information into another resource.

In the following example I am trying that a valid user(i.e Name = bipul and Password= bipul) can only be logged in. So, if a user will give the incorrect information(i.e Either Name or Password is given other than the bipul) in the form then the servlet will redirect again to the login form including the error message.



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
<form method="get" action="RequestIncludeExample">
<p>Give the following details</p>
Name <input type="text" name="name"/><br>
Password <input type="password" name="psw"/><br>
<input type="submit" value="submit"/>


package roseindia.requestDispatcherExample;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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

@WebServlet(name= "RequestIncludeExample", urlPatterns= {"/RequestIncludeExample"})
public class RequestIncludeExample extends HttpServlet {
private static final long serialVersionUID = 1L;

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("name");
String psw= request.getParameter("psw");

Connection con;
PreparedStatement ps;
ResultSet rs;

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

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

rs= ps.executeQuery();
String n= rs.getString("name");
out.println("Welcome "+n);
ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/login.html");
out.println("<font color=red>invalid user name or password</font>");
rd.include(request, response);


catch(SQLException sx)
catch(ClassNotFoundException cx)
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException 
doGet(request, response);

Output :

login table :

When you will execute this example you will get the output as :

When you will enter the value the like as :

After filling the information and if the filled information is wrong then the login page will be redirected again including the message written in servlet for error prone.

Download Source Code


Share on Google+Share on Google+

@WebServlet RequestDispatcher Include Example

Posted on: December 16, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: @WebServlet RequestDispatcher Include Example  

Post your Comment

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