
We use the WAR file in Java to package the web application which may contains servlet, xml, jsp, image, html, css, js etc. It has .war extension. A WAR file can be digitally signed just like a JAR file to allow others to determine where the source code came from.
Table of Contents
What resources are usually involved in WAR file?
A WAR usually contains following resources:
- Servlets, JavaServer Pages (JSP), Custom Tag Libraries.
- Server-side utility classes (database beans, shopping carts, and so on).
- Static web resources (HTML, image, and sound files, and so on).
- Client-side classes (applets and utility classes).
What is the directory structure of a web application in Java?
The directory structure of a web application consists of two parts. The first part is the public directory structure consisting of HTML/XML documents, JSP pages, images, applets, etc. The second part is a special WEB-INF directory.
The WEB-INF directory that contains the following files and directories:
- web.xml (The web application deployment descriptor.)
- Tag Library Descriptor files.
- classes/ (A directory that contains server-side classes: servlet, utility classes, and JavaBeans components.)
- lib/ (A directory that contains JAR archives of libraries.)
- tags/ (A directory that contains Tag files)
The structure of the web application deployment descriptor file looks like this:
WEB-INF/
WEB-INF/web.xml
WEB-INF/classes/
WEB-INF/lib/
WEB-INF/tags/
What are the advantages of WAR file?
There are following advantages of WAR files:
- Web applications become easier to test and deploy.
- Identifying the version of the deployed application becomes easy.
- Makes smooth deployment.
- WAR files are supported by all Java EE containers.
- MVC structure supports WAR files.
How to create a .war file?
jar cvf java_webapp.war *
Here, java_webapp is the web application name. -c creates file, -v to generate the verbose output and -f to specify the archive file name.
Difference between WAR and JAR file in Java
- You will need a web server or web container such as Tomcat or WebLogic or WebSphere to execute WAR file. A simple JDK is sufficient to execute a JAR file.
- WAR has a predefined structure with WEB-INF and META-INF directories, whereas you can create JAR with any structure.
- You can use WAR files only for web application where you can use JAR files to package library, plugin, or any types of application.
Also Read:
What are the APIs in Servlet?
Create servlet in Java (Steps)