Web.xml file for our HelloClient servlet example. (For details about working with Java and XML, see Chapter 24.) Let’s start with a simple XML, but you can simply copy these examples in a cut-and-paste fashion.
We will assume that you have at least a passing familiarity with Next, there may be some residualĬonfiguration, especially relating to options of the servlet container, To ignore the annotations completely, using an attribute called In general, configuration in the XML will take The XML, you can change configuration at deployment time without Used to override or extend the hardcoded annotation configuration. For most purposes, you will find it easier to use theĪnnotations, but there are a couple of reasons to understand the XMLĬonfiguration as well. In this section we will describe both the XML and annotation style
Another option with the Servlet 3.0ĪPI is to deploy servlet procedurally-using Java code at runtime. The servlet to the Tomcat server without any WebServlet annotation used in the first example, HelloClient, to declare the servlet and MostĬonfiguration can now be done using Java annotations. However, as of the Servlet 3.0 API, there are additional options. For most of the history of Java webĪpplications, this was the only deployment configuration mechanism. Their initialization parameters, and their deployment details, including
That the classes there will be loaded by a single classloader and madeĬonfiguration with web.xml and AnnotationsĬonfiguration file that lists servlets and related entities to beĭeployed, the relative names (URL paths) under which to deploy them, Requires that each server provide a directory for these extension JARs and If changed-a feature of WAR files that we’ll discuss later. The location for placing the libraries, however, is not standard andĪny classes that are deployed in this way cannot be automatically reloaded This is often done for common libraries that will be used by many webĪpps.
It’s also possible to install JAR files directly in the servletĬontainer to make them available to all web apps running on that server. Lib directory for supporting classes and third-party During development, it is often easier to work with the “loose” After that, any JAR files located in WEB-INF/lib areĪppended to the web app’s classpath (the order in which they are appended (using the normal Java package conventions) are available to theĪpplication. The classpath of the web application, so any class files placed here WEB-INF/classes directory is automatically added to The WEB-INF/classes and WEB-INF/lib directories contain JavaĬlass files and JAR libraries, respectively.
Web.xml file, which we’ll talk more about in the next However, so it is a safe place to store application resources. Your application classes can load additional files from this area using Server, and its contents are not visible to outside users of theĪpplication, even if you add WEB-INF to the base URL. Hyphenated) is a special directory that contains all deployment Our index.html file appears at theīase URL we just mentioned, and our happybunny.gif The top level of the WAR file becomes the document root (base directory)įor serving files. References to its documents, images, and servlets start with that path. Path of the web application-in this case, When deployed, the name of the WAR becomes, by default, the root
class WEB - INF / classes / com / mycompany / ReturnServlet. xml WEB - INF / classes / com / mycompany / PurchaseServlet. The contents of a typical WAR might look like this, as revealed by
The WAR file is simply a JAR file (which is itself a fancy ZIPįile) with specified directories for the Java code and one designatedĬonfiguration file: the web.xml file, which tells theĪpplication server what to run and how to run it.
As we described in the introduction to this chapter, a WAR file isĪn archive that contains all the parts of a web application: Java classįiles for servlets and web services, JSPs, HTML pages, images, and other