In a web application it is common to redirect the user to a web page. For example on successful authentication of a user, he/she is redirected to user home page. Otherwise, redirected to login page again. We have to ways to redirect a user: one is using the sendRedirect() method available on response object and the second way is to use the forward() method available in RequestDispatcher interface.
sendRedirect() is executed on client side, whereas forward() is executed on server side. sendRedirect() works only with HTTP, whereas forward() works with any protocol. sendRedirect() takes two request and one response to complete, where as only one call is consumed in the case of the forward() method. sendRedirect() can be used to redirect a user to a resource on any server on the web. Whereas forward() can be used to redirect a user to a resource on the same server where the application or the page resides.
Following example demonstrates redirecting a user using sendRedirect() and forward() methods:
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</title>
</head>
<body>
<form action="auth.html" method="get">
Username: <input type="text" name="txtuser" /><br />
Password: <input type="password" name="txtpass" /><br />
<input type="submit" value="Submit" />
<input type="reset" value="Clear" />
</form>
</body>
</html>
uhome.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>Insert title here</title>
</head>
<body>
<h1>User Homepage</h1>
</body>
</html>
Authenticate.java
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Authenticate
*/
public class Authenticate extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Authenticate() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("txtuser");
String password = request.getParameter("txtpass");
if(username.equals("vishnu") && password.equals("college"))
{
RequestDispatcher rd = request.getRequestDispatcher("uhome.html");
rd.forward(request, response);
//response.sendRedirect("uhome.html");
}
else
{
response.getWriter().print("Bad username or password");
}
}
}
In the above example, if the user is authenticated, he/she is redirected to uhome.html page.
Video: Redirection in Servlets
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply