Difference forward and sendRedirect
1. Forward
- Definition: forward is a server-side process. When you use , the request is transferred to another resource (such as a servlet, JSP, or HTML file) within the same server without the client being aware of the change.
- URL Change: The URL in the browser remains the same, as the client is not redirected.
- Performance: Generally faster since it doesn’t involve an additional round trip between the client and server.
- Usage: Often used for server-side navigation within an application, especially when the target resource is on the same server forward and sendRedirect.
2. SendRedirect
- Definition: sendRedirect is a client-side process. When you use sendRedirect, the client is instructed to make a new request to a different URL. This can be on the same server or a different server.
- URL Change: The URL in the browser changes to the new resource URL.
- Performance: Generally slower due to the extra round trip required for the client to make a new request.
- Usage: Often used to redirect to a resource on a different server, or when a complete new request is needed (for example, after form submission) forward and sendRedirect.

Table of Contents
Forward Example:
java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
public class ForwardExampleServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("targetPage.jsp");
dispatcher.forward(request, response);
}
}
SendRedirect Example:
java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
public class SendRedirectExampleServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect("http://www.example.com");
}
}