Explain ServletResponse vs sendRedirect
Both sendRedirect() and forward() are used to navigate to another resource, but they work differently and are used in different scenarios.
1. sendRedirect() Method:
- Client-Side Redirect:  The sendRedirect() method sends a response back to the client’s browser, instructing it to make a new request to the specified URL. This causes the client to see a new URL in the browser.
- New Request: Â Because the browser initiates a new request, any request attributes and parameters are lost unless they are appended to the URL.
- Scope: Â sendRedirect() can be used to redirect to resources within the same web application or to an external resource (different server, different context).
 2. forward() Method:
- Server-Side Forward: Â The forward() method is handled entirely on the server side. The server forwards the request to another resource within the same context without the client being aware of it. The URL in the browser remains unchanged.
- Same Request: Â The original request is preserved, so any request attributes and parameters remain available to the forwarded resource.
- Scope: Â forward() can only be used to forward requests to resources within the same web application context.

Table of Contents
Servlet using sendRedirect():
1. Servlet using sendRedirect():
java
import java.io.IOException;
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("/sendRedirectExample")
public class SendRedirectExampleServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String newURL = "http://www.example.com";
response.sendRedirect(newURL);
}
}
Example 2: Using forward()
1. Servlet using forward():
java
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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("/forwardExample")
public class ForwardExampleServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("/targetServlet");
request.setAttribute("message", "Hello from ForwardExampleServlet");
dispatcher.forward(request, response);
}
}
Target Servlet:
2. Target Servlet:
java
import java.io.IOException;
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("/targetServlet")
public class TargetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String message = (String) request.getAttribute("message");
response.setContentType("text/html;charset=UTF-8");
response.getWriter().println("<html><body>");
response.getWriter().println("<h2>" + message + "</h2>");
response.getWriter().println("</body></html>");
}
}
Differences ServletResponse vs sendRedirect
- Redirect (sendRedirect()):
- Client-side redirection.
- Initiates a new request.
- URL changes in the browser.
- Can redirect to external resources.
- Forward (forward()):
- Server-side forwarding.
- Uses the same request.
- URL remains unchanged in the browser.
- Limited to resources within the same web application context.