A.15. Integrating Servlets and JSP

Big Picture

  • Servlet handles initial request, reads parameters, cookies, session information, etc.

  • Servlet then does whatever computations and database lookups are needed.

  • Servlet then stores data in beans.

  • Servlet forwards request to one of many possible JSP pages to present final result.

  • JSP page extracts needed values from beans.

Request Forwarding Syntax

String url = "/path/presentation1.jsp";
RequestDispatcher dispatcher =
  getServletContext().getRequestDispatcher(url);
dispatcher.forward();

Forwarding to Regular HTML Pages

  • If initial servlet handles GET requests only, no change is necessary.

  • If initial servlet handles POST, then change destination page from SomePage.html to SomePage.jsp so that it, too, can ...

Get Core Servlets and JavaServer Pages™ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.