Chapter 20. Servlets and JavaServer Pages

A servlet is a Java class that implements the javax.servlet.Servlet interface or, more commonly, extends the abstract javax.servlet.http.HttpServlet class. Servlets, and the Java Servlet API, are an extension architecture for web servers.[1] Instead of serving only static web pages, a servlet-enabled web server can invoke servlet methods to dynamically generate content at runtime. This model offers a number of advantages over traditional CGI scripts. Notably, servlet instances can persist across client requests, so the server is not constantly spawning external processes.

JavaServer Pages (or JSP as it is commonly called) is an architecture built on top of the servlet API. A JSP page contains HTML or XML output intermingled with Java source code, special JSP tags, and tags from imported “tag libraries,” including the very useful Java Standard Tag Library, or JSTL. In JSP 2.0, JSP pages may also contain expressions written in a simple “Expression Language” (EL); these expressions are evaluated and are replaced with their values. A JSP-enabled web server compiles JSP pages on the fly, turning JSP source into servlets that produce dynamic output.[2]

This chapter includes examples of both servlets and JSP pages and concludes with an example “web application” that uses Java objects, JSP pages, and a coordinating servlet in a Model-View-Controller (MVC) architecture. The chapter begins, however, by describing the prerequisites for compiling, ...

Get Java Examples in a Nutshell, 3rd Edition 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.