MTI TEK
  • Home
  • About
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All Resources
Java Servlet | WAR (Web Application Archive) Files
  1. What is a WAR File
  2. Creating a WAR File
  3. Extracting a WAR File
  4. Deploying WAR Files

  1. What is a WAR File
    A WAR file is a compressed archive format used to package and distribute Java web applications. It is essentially a JAR file with a .war extension that contains all the components of a web application in a single deployable unit.

    WAR files contain the complete directory structure of a web application, including:
    • Web pages (HTML, JSP files)
    • Static resources (CSS, JavaScript, images)
    • Java classes and libraries
    • Configuration files (web.xml)
    • The entire WEB-INF directory structure
    The main advantages of using WAR files include simplified deployment, version control, and the ability to distribute web applications as single files across different servlet containers.
  2. Creating a WAR File
    To create a WAR file from your web application directory, use the jar command-line tool. Navigate to your web application's root directory and execute the following command:
    jar cvf0 mywarfile.war *.*
    This command creates a WAR file named `mywarfile.war` containing all files and subdirectories in the current directory.

    Parameters of the `jar` command:
    • c: creates a new archive file.
    • v: enables verbose mode to display detailed information about the files being processed.
    • f: specifies that the archive filename will be provided as an argument.
    • 0: (zero) stores files without compression for faster deployment and extraction.
    • mywarfile.war: the name of the WAR file to create.
    • *.*: specifies all files in the current directory to include in the archive.
    For better organization, you can also specify individual directories or use patterns like `*.jsp` to include only specific file types. Most servlet containers can handle both compressed and uncompressed WAR files.
  3. Extracting a WAR File
    To extract the contents of a WAR file, use the jar command with the extract option:
    jar xvf mywebapp.war
    This command extracts all files from `mywebapp.war` into the current directory, preserving the original directory structure of the web application.

    Parameters of the `jar` command:
    • x: extracts files from the specified archive.
    • v: enables verbose mode to display the names of files being extracted.
    • f: indicates that the archive filename follows as an argument.
    • mywebapp.war: the name of the WAR file to extract.
    You can also extract specific files or directories by specifying their paths after the WAR filename. The extracted files will maintain their original directory structure, including the WEB-INF directory and all its contents.
  4. Deploying WAR Files
    WAR files are designed for easy deployment to servlet containers. Most servlet containers support automatic deployment by simply copying the WAR file to a designated deployment directory.

    Common deployment methods include:
    • Hot deployment: Copy the WAR file to the container's deployment directory while the server is running.
    • Administrative interface: Use the container's web-based management console to upload and deploy WAR files.
    • Command-line tools: Use container-specific deployment commands or scripts.
    When a WAR file is deployed, the servlet container automatically extracts its contents and makes the web application available at a URL path that typically matches the WAR filename. For example, `myapp.war` would be accessible at `http://localhost:8080/myapp/`.
© 2025 mtitek