@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.

@WebServlet RequestDispatcher Include Example

@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.

Example

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="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"/>
</form>
</body>
</html>

RequestIncludeExample.java

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

Connection con;
PreparedStatement ps;
ResultSet rs;

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

String sql= "SELECT name FROM login 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("/login.html");
out.println("<font color=red>invalid user name or password</font>");
rd.include(request, response);
}

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

catch(SQLException sx)
{
out.println(sx);
}
catch(ClassNotFoundException cx)
{
out.println(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