# WAR (file format)

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/WAR_(file_format)
> Markdown URL: https://mediated.wiki/source/WAR_(file_format).md
> Source: https://en.wikipedia.org/wiki/WAR_(file_format)
> Source revision: 1285316582
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Type of file format}}
{{Infobox file format
| name = Web ARchive
| extension = .war
| mime = 
| magic = <code>PK\x03\x04</code> (standard ZIP file)
| developer = {{hlist|[Sun](/source/Sun_Microsystems)|[Oracle](/source/Oracle_Corporation)|[Eclipse](/source/Eclipse_Foundation)}}
| creatorcode = 
| genre = 
| containerfor = [JSP](/source/Jakarta_Server_Pages), [Jakarta Servlet](/source/Jakarta_Servlet)
| containedby = 
| extendedfrom = [JAR](/source/JAR_(file_format))
| extendedto = 
}}
In [software engineering](/source/software_engineering), a '''WAR''' file ('''W'''eb '''A'''pplication '''R'''esource<ref>{{cite web |url=https://tomcat.apache.org/tomcat-8.0-doc/deployer-howto.html |title=Apache Tomcat 8 (8.0.44) - Tomcat Web Application Deployment |last=Crossley |first=Allistair |publisher=[Apache Software Foundation](/source/Apache_Software_Foundation) |access-date=2017-06-27}}</ref> or '''W'''eb application '''AR'''chive<ref>{{cite web |url=https://www.infoworld.com/article/2076518/what-s-new-in-java-servlet-api-2-2-.html |title=What's New in Java Servlet API 2.2? |publisher=[JavaWorld](/source/JavaWorld)|first=Jason|last=Hunter|date=1999-10-15 |access-date=2020-11-08}}</ref>) is a file used to distribute a collection of [JAR](/source/JAR_(file_format))-files, [Jakarta Server Pages](/source/Jakarta_Server_Pages), [Jakarta Servlet](/source/Jakarta_Servlet)s, [Java](/source/Java_(programming_language)) [class](/source/class_(file_format))es, [XML](/source/XML) files, tag libraries, static web pages ([HTML](/source/HTML) and related files) and other resources that together constitute a [web application](/source/web_application).

==Content and structure==
A WAR file may be [digitally signed](/source/digital_signature) in the same way as a [JAR](/source/JAR_(file_format)) file in order to allow others to determine where the source code came from.

There are special files and directories within a WAR file:

* The /WEB-INF directory in the WAR file contains a file named [web.xml](/source/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](/source/Jakarta_Servlet) a [URL](/source/Uniform_Resource_Locator) request will be routed. The web.xml file 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 Jakarta EE containers support WAR files
* [MVC](/source/Model%E2%80%93view%E2%80%93controller) structure supports WAR files.

Assuming production environments do not promote a fix without sufficient testing prior to deployment, a WAR file has a distinct advantage when properties files are used to identify environment specific variables. For example, an LDAP server in a testing environment may be something like <code>ldaps://testauth.example.com:636</code>. The LDAP server in a production environment is <code>ldaps://auth.example.com:636</code>. An external properties file would define the link with some thing like:

 LINKED_PAGE=ldaps://testauth.example.com:636

The source code reads the property file to determine the target LDAP server. In this way, developers can be certain that the WAR file tested and verified is exactly the same as that which is being promoted to production.

== Disadvantages of WAR files ==
Some{{who|date=May 2024}} consider web deployment using WAR files to be disadvantageous when minor changes to source code are required for dynamic environments. Each change to source code must be repackaged and deployed in development.<ref>{{cite web |title=Web Application Lifecycle |url=https://docs.oracle.com/javaee/6/tutorial/doc/bnadu.html |publisher=[Oracle](/source/Oracle_Corporation) |work=The Java EE 6 Tutorial}}</ref> This does not require stopping the web server if configured for runtime deployment.<ref>{{cite web |title=Deploying on a running Tomcat server |url=https://tomcat.apache.org/tomcat-6.0-doc/deployer-howto.html#Deploying_on_a_running_Tomcat_server |publisher=[Apache Software Foundation](/source/Apache_Software_Foundation)}}</ref>

== Example ==
The following sample ''web.xml'' file demonstrates the declaration and association of a [servlet](/source/Jakarta_Servlet):

<syntaxhighlight lang="xml">
<?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>
</syntaxhighlight>

The <code>/WEB-INF/classes</code> directory is on the [class loader](/source/Java_class_loader)'s [classpath](/source/classpath_(Java)). (The classpath consists of a list of locations from which <code>.class</code> files can be loaded and executed by the JVM.) The <code>/WEB-INF/classes</code> directory contains the classes associated with the web application itself.

Any JAR files placed in the <code>/WEB-INF/lib</code> directory will also be placed on the class loader's classpath.

== See also ==
* [EAR (file format)](/source/EAR_(file_format))
* [JAR (file format)](/source/JAR_(file_format))

==References==
{{Reflist}}

== External links ==
* [https://jakarta.ee/learn/docs/jakartaee-tutorial/9.1/platform/packaging/packaging003.html Packaging Web Archives] (The Jakarta EE 9.1 Tutorial)
* [https://jcp.org/en/jsr/detail?id=154 JSR 154: JavaTM Servlet 2.4 Specification]

{{Archive formats}}

{{DEFAULTSORT:War File Format (Sun)}}
Category:Archive formats
Category:Java enterprise platform

---
Adapted from the Wikipedia article [WAR (file format)](https://en.wikipedia.org/wiki/WAR_(file_format)) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/WAR_(file_format)?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
