Web Applications

Now that we’ve seen a basic servlet, we can step back for a moment and talk about how servlets are integrated into the servlet container. Version 2.2 of the Servlet API popularized the concept of a web application installed within a web server. A web application consists of a set of resources, including servlets, static content, JSP files, and class libraries, installed within a particular path on a web server. This path is called the servlet context, and all servlets installed within the context are given an isolated, protected environment to operate in, without interference from (or the ability to interfere with) other software running on the server.

A servlet context directory tree contains several different types of resources. These include class files and JAR files (which aren’t exposed to clients connecting via web browsers), JSP files (which are processed by the JSP servlet before being fed back to the client), and static files, such as HTML documents and JPEG images, which are served directly to the browser by the web server.

Finally, there is a virtual component to the context. For each context, the servlet container will instantiate separate copies of servlets (even if those servlets are shared) and will create a private address space that can be accessed via the ServletContext class. Servlets can use this class to communicate with other servlets running in the same context. We’ll discuss this more later.

The simplest servlet installations will just ...

Get Java Enterprise in a Nutshell, Second 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.