Sunday, 20 March 2016

How to deploy a Java web application on Tomcat ?


Important points before starting the deployment

While working with deployment of Java web applications on Tomcat, you need to know :
$CATALINA_HOME : Environment variable points to the directory where you installed Tomcat. For example, c:\Program Files\Apache Software Foundation\Tomcat 7.0 on Windows.

$CATALINA_BASE : Environment variable points to the directory of a particular instance of Tomcat (if you configure multiple instances of Tomcat). If this variable is not set explicitly, then it will be assigned the same value as $CATALINA_HOME

$CATALINA_HOME\webapps : Web applications are put under this directory.

Document root : Top-level directory of a web application, where all the resources (JSP pages, HTML pages, Java classes, images…) that constitute that application are placed.

Context path : Name which is relative to the server’s address (i.e http://localhost>) and represents the name of the web application.
For example, if your web application is put under $CATALINA_HOME\webapps\MyWeb directory, it will be accessed by the URLhttp://localhost/MyWeb, and its context path is /MyWeb.

WAR : Extension of a file that packs a web application directory hierarchy in ZIP format.
WAR stands for Web Archive. Java web applications are usually packed in WAR files for deployment.
WAR files can be created by command line or an IDE like Eclipse.
You must have a strong understanding of how a Java web application is organized in terms of directory layout and hierarchy structure.

$CATALINA_HOME\lib : JAR libraries which are shared among web applications are put under this directory.
Application-specific JAR libraries are put under web application’s WEB-INF\lib directory.


Deployment on Tomcat

Deployment method #1: Copying web application archive file (.war)
Web application is packed as a WAR file. You may generate the WAR file using a tool or IDE like Eclipse, or you already have a WAR file.
 1. Copy the WAR file into $CATALINA_HOME\webapps directory.
 2. Restart the server.
Whenever Tomcat is started, it will unpack the WAR file it found in the webapps directory and launch the application in that manner.
NOTE : Later if you want to update changes for the application, you must replace the WAR file and delete the application’s unpacked directory, and then restart Tomcat.

Deployment method #2: Copying unpacked web application directory
You have the web application in its unpacked form.
 1. Copy the application’s directory from its location into $CATALINA_HOME\webapps directory.
 2. Restart the server, the application is deployed with the context path is name of the directory you copied.
NOTE : If you want to update changes for the application, you must replace the corresponding files under its document root directory.

Deployment method #3: Using Tomcat’s manager application
You can deploy the web application remotely via a web interface provided by Tomcat’s manager application.
You must have user name and password to access this application. The manager application is installed by default, but not always. So be sure that it is installed with your version of Tomcat.
Features of manager application :
 1. View a list of applications deployed on the server and their status.
 2. Start, stop and restart an individual application.
 3. Deploy a new web application either by uploading a WAR file or supplying a directory on the server.
 4. Undeploy an individual application.

By default, the manager application is deployed under context /manager, so to access it :
 1. Type the following URL into your web browser’s address bar (the port number may vary, depending on your server’s configuration) :
      http://localhost:8080/manager
 2. Supply correct user name and password.
 3. The list of deployed applications is shown at the top, Scroll down to see deployment section.
 4. Click Browse button to pick up a WAR file and click Deploy button.
As soon as the WAR file is uploaded to the server, it is unpacked into $CATALINA_HOME\webapps directory. The manager adds the newly deployed application to the list of applications.


Accessing the deployed application

Typically, a web application can be accessed by typing its context path follows the server’s IP / domain (including port number if any).
Example : http://localhost:8080/MyApp

No comments:

Post a Comment

Note: only a member of this blog may post a comment.