WAR (file format)
|Developed by||Sun Microsystems|
|Container for||JSP, Java Servlet|
In software engineering, a WAR file (or Web application ARchive) is a JAR file used to distribute a collection of JavaServer Pages, Java Servlets, Java classes, XML files, tag libraries, static web pages (HTML and related files) and other resources that together constitute a web application.
Content and structure
There are special files and directories within a WAR file:
- The /WEB-INF directory in the WAR file contains a file named web.xml which defines the structure of the web application. If the web application is only serving JSP files, the web.xml file is not strictly necessary. If the web application uses servlets, then the servlet container uses web.xml to ascertain to which servlet a URL request will be routed. web.xml is also used to define context variables which can be referenced within the servlets and it is used to define environmental dependencies which the deployer is expected to set up. An example of this is a dependency on a mail session used to send email. The servlet container is responsible for providing this service.
Advantages of WAR files
- Easy testing and deployment of web applications
- Easy identification of the version of the deployed application
- All Java EE containers support WAR files
Disadvantages of WAR files
One disadvantage of web deployment using WAR files (especially in very dynamic environments) stems from the restriction that minor changes cannot be made during runtime. Any change whatsoever requires regenerating and redeploying the entire WAR file.
The following sample web.xml file demonstrates the declaration and association of a servlet:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>mypackage.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/HelloServlet</url-pattern> </servlet-mapping> <resource-ref> <description> Resource reference to a factory for javax.mail.Session instances that may be used for sending electronic mail messages, preconfigured to connect to the appropriate SMTP server. </description> <res-ref-name>mail/Session</res-ref-name> <res-type>javax.mail.Session</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app>
Any JAR files placed in the /WEB-INF/lib directory will also be placed on the ClassLoader's classpath.
Related file formats:
- Oracle Java EE 7 Tutorial: Packaging Web Archives
- Oracle Java EE 6 Tutorial: Web Modules
- Oracle Java EE 5 Tutorial: Web Modules
- Sun Microsystems: XML Schema for the Servlet 2.5 Web ARchive (WAR) File
- Sun Microsystems: XML Schema for the Servlet 2.4 Web ARchive (WAR) File
- JSR 154: Java Servlet 2.4 Specification