Home Java Javaee6 @WebServlet RequestDispatcher Forward Example
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

@WebServlet RequestDispatcher Forward Example

Advertisement
In this tutorial you will learn how forward to the another resources in the response.

@WebServlet RequestDispatcher Forward Example

In this tutorial you will learn how forward to the another resources in the response.

Sometimes it is required to transfer the control to another web component. In such condition you will be required to forward directly to the another resource's/component in the response that is returned from a web component. forward(request, response) method of RequestDispatcher is used for forwarding to another resource's or web component. The example is given below will demonstrate you to how can you forward to another resource/component in the response.

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 gives the correct information then the control will be transferred to another servlet otherwise, (i.e Either Name or Password is given other than the bipul) servlet will be redirected again to the login form including the error message.

Example

login1.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="RequestForwardExample">
<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>

RequestForwardExample.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;
import javax.servlet.http.HttpSession;

@WebServlet(name= "RequestForwardExample",
urlPatterns= {"/RequestForwardExample"})
public class RequestForwardExample extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out= response.getWriter();

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

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

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())
{
HttpSession session= request.getSession();
String n= rs.getString("name");
session.setAttribute("name", n);

ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/ForwardTesterServlet");
rd.forward(request, response);
}
else
{
ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/login1.html");
out.println("<font color=red>invalid user name or password</font>");
rd.include(request, response);
}

con.close();
ps.close();
rs.close(); 
}
catch(ClassNotFoundException cx)
{
out.println();
}
catch(SQLException sx)
{
out.println();
}
}
}

ForwardTesterServlet.java

package roseindia.requestDispatcherExample;

import java.io.IOException;
import java.io.PrintWriter;

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;
import javax.servlet.http.HttpSession;

@WebServlet("/ForwardTesterServlet")
public class ForwardTesterServlet 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();
HttpSession session= request.getSession();
String name= (String)session.getAttribute("name");
out.println("Welcome "+name);
out.println("<h4>You are successfully logged in<h4>");

}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}

Output :

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

When you will enter the correct value into their respective field as :

After then when you will click on submit value the output will be given as :

Download Source Code

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 16, 2011

Ask Questions?    Discuss: @WebServlet RequestDispatcher Forward Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com