LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 1819 min read

Forwarding Requests (jsp:forward)

Understand server-side forwarding with jsp:forward versus a client-side redirect, and how to pass request attributes along with a forward.

Introduction

Not every request should be handled entirely by the page it first hits. A search page might need to hand processing off to a results page; a servlet that validates a form might need to hand rendering off to a JSP. JSP provides jsp:forward for exactly this: passing the current request, untouched, to a different resource on the server.

What You Will Learn
  • How jsp:forward hands off a request server-side.
  • The key differences between forward and response.sendRedirect.
  • How to pass request attributes along with a forward.
  • How a servlet typically forwards to a JSP view.

The jsp:forward Action

jsp:forward transfers control to another resource within the same application. The original request and response objects are reused — nothing is sent back to the browser first, and the browser's URL bar does not change.

<!-- search.jsp -->
<%
String query = request.getParameter("q");
if (query == null || query.trim().isEmpty()) {
%>
<jsp:forward page="empty-query.jsp" />
<%
}
%>
<p>Showing results for: <%= query %></p>

Forward vs Redirect

A redirect (response.sendRedirect()) is fundamentally different: it sends a 302 response back to the browser telling it to make a brand-new request to a different URL. That means a redirect is a round trip to the client and back, while a forward never leaves the server.

Aspectjsp:forward (Server-side)sendRedirect (Client-side)
Request/response usedSame request and response objectsA new request/response from the browser
Browser URLStays the same (unchanged)Changes to the new URL
Round tripNone — stays on the serverOne extra round trip (302 then new GET)
Request attributesPreserved (visible on the target)Lost (new request has none)
Can target external sitesNo — same application onlyYes — any URL
Rule of Thumb

Use forward when handing off internally to render a view for the current request. Use redirect when you want the browser's URL to change, or after a form submission to prevent duplicate resubmission on refresh (the well-known Post/Redirect/Get pattern).

Passing Request Attributes

Because a forward reuses the same request object, anything stored with setAttribute() before the forward is still readable on the target page — this is the standard way to pass computed data into a view.

<%
// process.jsp
java.util.List<String> results = java.util.Arrays.asList("Course A", "Course B", "Course C");
request.setAttribute("results", results);
%>
<jsp:forward page="results-view.jsp" />
results-view.jsp Reading the Forwarded Attribute
<!-- results-view.jsp -->
<ul>
<c:forEach var="item" items="${results}">
<li>${item}</li>
</c:forEach>
</ul>
Rendered Output

Click Run to see what this code prints.

Forwarding from a Servlet

The same mechanism is used constantly from Java servlets, via RequestDispatcher, and is the backbone of the MVC pattern you will build in a later lesson.

Forwarding from a Servlet with RequestDispatcher
// SearchServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("results", searchService.search(request.getParameter("q")));
RequestDispatcher dispatcher = request.getRequestDispatcher("/results-view.jsp");
dispatcher.forward(request, response);
}

Common Mistakes

Avoid These Mistakes
  • Writing to the response (e.g. out.println) before forwarding — the container may throw an IllegalStateException since output has already started.
  • Expecting the browser URL to change after a forward — it never does, which can confuse users on bookmarking or refresh.
  • Using forward after a form POST without any redirect, causing a refresh to resubmit the form.
  • Trying to forward to an external website — forward only works within the same web application.

Best Practices

  • Use forward to hand off from a controller servlet to its JSP view.
  • Use redirect after any state-changing POST to avoid duplicate submissions on refresh.
  • Set all request attributes before calling forward, not after.
  • Keep forwarded resources inside the same logical module for clarity.

Frequently Asked Questions

No, it can only target resources within the same web application deployed on the server.

Technically only up to the point where the response has been committed; once output is flushed to the browser, a further forward will fail.

Yes, jsp:forward is simply the JSP-tag equivalent of calling RequestDispatcher.forward() from Java code.

Key Takeaways

  • jsp:forward hands off the current request to another resource on the server.
  • Forward keeps the same request/response and the browser URL never changes.
  • Redirect makes the browser issue a brand-new request to a new URL.
  • Request attributes survive a forward but not a redirect.

Summary

Forwarding is the glue that connects controllers to views in real applications. Before you build a full controller, though, you need a clean way to represent data — that is where JavaBeans come in, covered next.

Next Lesson →

JavaBeans in JSP