Understanding java web application deployment
Deployment is the process of uploading a web application on a web server in such a format that it can be managed and executed by the server. Servlet API, defines standard formats for packaging and distributing java web applications.
A Java web application can be deployed in the following two formats on a server:
- Exploded Format
- Packaged Format
In exploded format, application folder which contains application components as per the standard directory structure, is used as the deployment unit i.e. the application folder is uploaded on the server. The advantage of this approach is that after deployment when some modifications are made to the application components, these modifications are auto applied by the server on the deployed application. Usually packaged format is used during development time when frequent modifications are made to the application.
In packaged format, contents of the application folder are compressed in a Web Application Archive (WAR) file which is used as the deployment unit i.e the war file is uploaded on the server. Packaged format is used at the time of product release i.e. when the application is moved from the development server to the production server. If after deployment, application is modified, war file need to be created and deployed again to reflect changes on the server.
Creating a war file:
A war file is created with the help of jar tool which is part of JDK. To create a war file following steps are required:
- Arrange the application contents according to the standard directory structure.
- Change folder to the application folder.
- Use the jar command to compress the contents in a war file as follows:
The -c option represents that a new jar or war file is to be created, -v option represents verbose (wordy, descriptive) execution of the jar tool i.e. the log of the compression process is to be displayed on the console, the -f option represent the name of the jar or war file to be created and the * option represents that all the files and folders of the current folder including their contents are to be compressed.
Deploying an application:
Each server supports following two modes of deployment:
- Hot Deployment
- Cold Deployment
In hot deployment, application folder or its war file is directly copied to the staging folder of the server. Staging folder is the folder which is used by the server for launching applications. The name and path of this folder differs from server to server e.g. in Tomcat the staging folder is webapps. In this approach, the application programmer need to be familiar with the server directory structure.
In cold deployment, application folder or its war file is uploaded on the server with the help of a mediator which is usually some application. For example, all servers provides admin consoles for deploying and managing applications. These consoles are web applications which provide browser (HTML) based interface to the application programmers. Tomcat manager is the application console of Tomcat. In this approach, application programmers need not be familiar with the directory structure of the server.