Hackforge Academy

Category: java

Request dispatcher forward and include

Published on 30 Sep 2026

Explanation

RequestDispatcher allows a servlet to transfer a request to another resource such as another servlet, JSP, or HTML page.

Code:

RequestDispatcher dispatcher =
    request.getRequestDispatcher("home.jsp");

Explanation

forward() transfers request processing to another resource. The browser URL normally remains unchanged.

Code:

RequestDispatcher dispatcher =
    request.getRequestDispatcher("home.jsp");

dispatcher.forward(request, response);

Explanation

include() includes the output of another resource in the current response.

Code:

RequestDispatcher dispatcher =
    request.getRequestDispatcher("header.jsp");

dispatcher.include(request, response);

Explanation

Forward is commonly used by a controller servlet to send data to a JSP view.

Code:

request.setAttribute("user", user);

RequestDispatcher dispatcher =
    request.getRequestDispatcher("profile.jsp");

dispatcher.forward(request, response);

Explanation

Include is useful when common page sections such as headers, menus, and footers need to be reused.

Code:

out.println("<html>");

request.getRequestDispatcher("header.jsp")
       .include(request, response);

out.println("<main>Content</main>");

request.getRequestDispatcher("footer.jsp")
       .include(request, response);

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community