When a web application needs to instruct the client to fetch a different resource, the server can respond with a 302 statuss and a Location header. The Java Servlet API encapsulates this behavior through HttpServletResponse.sendRedirect(). Unlike server-side forwarding, a redirect causes the browser to issue a completely new request, making the target URL visible in the address bar.
How Redirection Works
- The browser sends a request to a servlet (e.g.,
/source). - The servlet calls
sendRedirect("/target?key=value"). The container sets the response status to302 Foundand inserts theLocationheader with the given path. - The browser receives the 302 response and automatically makes a new GET request to the URL specified in
Location. - The target resource processes the new request and returns its response.
During this process, the original HttpServletRequest and HttpServletResponse objects are discarded. Any attributes stored via request.setAttribute() are lost. However, simple data can be transmitted as query parameters in the redirect URL.
Code Examples
Source Servlet
The following servlet reads a parameter (amount) and redirects the client to a second servlet, appending the value to the destination URL. It also demonstrates redirecting to an external site (commented out) and the inability to directly acess resources inside WEB-INF.
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/redirectSource")
public class RedirectSourceServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
System.out.println("RedirectSource invoked");
String amount = req.getParameter("amount");
System.out.println("amount = " + amount);
// Internal redirect passing data as a query parameter
resp.sendRedirect("redirectTarget?amount=" + amount);
// External redirect
// resp.sendRedirect("https://example.com");
// Cannot redirect to a resource inside WEB-INF directly
// resp.sendRedirect("WEB-INF/private.html");
}
}
Target Servlet
The second servlet extracts the query parameter and logs it. Because it is a new request, the servlet can be completely independent of the source.
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/redirectTarget")
public class RedirectTargetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("RedirectTarget invoked");
String receivedAmount = req.getParameter("amount");
System.out.println("received amount = " + receivedAmount);
}
}
Key Points
sendRedirect()triggers a browser-initiated navigation; the URL in the address bar changes.- Each redirect results in a new request and new response objects.
- Request attributes are not preserved; send simple data via query strings.
- Redirects can navigate to external domains, wich is not possible with a server-side forward.
- Resources under
WEB-INFare not directly accessible via a redirect because the browser cannot reach them directly; they require a forward.