Difference Between Forward And sendRedirect In Servlet

First let us list the differences between the forward() and sendRedirect() methods. Then we will see an example for each:

forward()sendRedirect()
The forward() method is executed in the server side.The sendRedirect() method is executed in the client side.
The request is transfer to other resource within same server.The request is transfer to other resource to different server.
It does not depend on the client’s request protocol since the forward ( ) method is provided by the servlet container.The sendRedirect() method is provided under HTTP so it can be used only with HTTP clients.
The request is shared by the target resource.New request is created for the destination resource.
Only one call is consumed in this method.Two request and response calls are consumed.
It can be used within server.It can be used within and outside the server.
We cannot see forwarded message, it is transparent.We can see redirected address, it is not transparent.
The forward() method is faster than sendRedirect() method.The sendRedirect() method is slower because when new request is created old request object is lost.
It is declared in RequestDispatcher interface.It is declared in HttpServletResponse.
Signature :
forward(ServletRequest request, ServletResponse response)
Signature:
void sendRedirect(String url)

Example for sendRedirect () method

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RedirectServlet extends HttpServlet {

	protected void doGet(HttpServletRequest req, HttpServletResponse res)

	throws ServletException, IOException {
		res.setContentType("text/html");
		PrintWriter out = res.getWriter();

		res.sendRedirect("http://www.javabeat.net");

		out.close();
	}

}

Example for forward () method

Hello.html

<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="Simple" method="get">
	Name: <input type="text" name="uname">
	password: <input type="password" name="upass"><br />
<input type="submit" value="Submit" />
</form>

</body>
</html>

SimpleServlet(servlet file)

package javabeat.net.servlets;

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

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SimpleServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("doGet--------------start");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		String str = request.getParameter("uname");
		String st = request.getParameter("upass");
		System.out.println("doGet--------------Middle");
		if (st.equals("javabeat")) {
			RequestDispatcher rd = request.getRequestDispatcher("Welcome");

			rd.forward(request, response);

		} else {
			out.print("Sorry username and password error!");
			RequestDispatcher rd = request.getRequestDispatcher("/Hello.html");
			rd.include(request, response);
		}
		System.out.println("doGet--------------end");
	}

}

Welcome (servlet file)

package javabeat.net.servlets;

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Welcome extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		String str = request.getParameter("uname");
		out.print("welcome " + str);
	}

}

Now execute the Hello.html,right mouse click select Run >Run As and enter “javabeat” and click on submit. A message “Welcome Javabeat” will be displayed on the screen.

Previous Tutorial : Http Status Code  || Next Tutorial : ServletInputStream and ServletOutputStream

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*