CALL 1.800.975.6814    LIVE CHAT

@dailyrazor Blog

How to Deploy Spring Application in Tomcat

March 2 2017 0
how to deploy spring application in tomcat

In this article you will learn how to deploy Spring application in Tomcat. As you already know, Spring MVC framework is a popular Java web development framework. Therefore, our objective is to show you how to easily deploy a Spring MVC web application on either our Private or Shared Tomcat hosting servers.

At this point, please take note of the following prerequisites:

  • Make sure your web application is working properly on your local Tomcat server.
  • Filezilla or WinSCP should be installed on your local system. Filezilla or WinSCP can be used to upload war files to tomcat server.
  • In case no FTP client is installed on your local system. Please download a FTP client such as Filezilla and install it on your local PC.
  • In this tutorial we will upload our files using Filezilla. For WinSCP, SmartFTP or any other FTP client, the process is quite similar.
  • You must have knowledge on how to upload files/folders to remote server using a FTP client.
  • If you need specific Java and Tomcat Server version, please inform to our support team via email at support@dailyrazor.com. We will configure it for you. We suggest to use the latest Java and Tomcat Server.

 
Once you meet the above mentioned prerequisites, you can start deploying your Spring MVC war file. How to deploy a WAR file on Shared Tomcat or Private Tomcat server is slightly different. We will discuss both ways in this article.
 

[SPECIAL OFFER]: 15 Days FREE Trial Java Web Hosting

[BENEFITS]:

  • Host Your Spring MVC Web Application for Free!
  • Private Tomcat Server with Dedicated JVM Heap & PermGen
  • Custom Java Development Kit (JDK) version
  • Super Fast Servers with 24/7/365 Technical Support

Click here to access this [SPECIAL OFFER]

 

How to Deploy Spring Application in Tomcat

 
Furthermore, it’s worth noting that the three most suggested and common methods to deploy a any Java web application on our Private Tomcat or Shared Tomcat servers are:

  • 1. Copying web application archive file (.war)
  • 2. Copying unpacked web application directory
  • 3. Using Tomcat Manager application

 
Accordingly, this article will describe all three methods to deploy a war file. First two methods (Point 1 & 2) are applicable for both Private and Shared Tomcat hosting users. So you can upload either your application web archive file (.war file) or unpacked web application directory. Either .war or complete web directory needs to be uploaded to “/home/user/tomcat/webapps/yourdomain.com/” directory (for Private Tomcat server) OR /home/user/webapps/ (for Shared Tomcat server). The third method (Point 3) of war deployment is applicable for only Private Tomcat users. Therefore, let us go through the three methods one by one.

Method #1: Copying web application archive file (.war) using Filezilla

This method on how to deploy Spring application in Tomcat is applicable for both Private and Shared Tomcat web hosting. The two main differences are:
First, in the case of Shared Tomcat hosting, the WAR file is not automatically deployed. You need to send an email to support@dailyrazor.com to restart the Tomcat server to automatically deploy it. Whereas, in the case of Private Tomcat hosting the WAR file gets deployed automatically after upload.
Second, there is a slight difference in the folder/directory structure where .WAR file is uploaded. In the case of Private Tomcat, you upload to “/home/user/tomcat/webapps/yourdomain.com/” folder/directory whereas with Shared Tomcat you upload to /home/user/webapps/ folder/directory.

Step 1: Open Filezilla in your local System.

how to deploy spring application in tomcat

Step 2: Login with the login credentials provided to you after server set up.

At this point, you need to enter your FTP information to connect to remote hosting server. For Host we suggest you to enter your server’s IP address. You can also enter your domain name if it’s already pointing to our servers. Once you have given all the information correctly. Click on Quickconnect icon to access the server.
“Directory listing successful” – This message will appear once Filezilla is successfully connected to the server. Once successfully connected, Filezilla is ready to communicate with the server. After successful connection Filezilla would look like below given image.
how to deploy spring application in tomcat
NOTE: In Filezilla, the left panel shows folders/directories in your local computer and the right Panel shows your remote host’s folders/directories.

Step 3: Locate the required folders on your local computer and the remote server.

First and foremost, locate the folder/directory on your local computer where your .war file resides:
We want to deploy SpringMVCDeploymentDailyRazor.war which is located at “D:\learning\spring-web-mvc\SpringMVCDeploymentDailyRazor\target\SpringMVCDeploymentDailyRazor.war ” in our local system. As a result, in the left panel of Filezilla, we’ll open “D:\learning\spring-web-mvc\SpringMVCDeploymentDailyRazor\target“.

Next, locate the webapps folder/directory on the remote server:
We need to upload SpringMVCDeploymentDailyRazor.war to “/home/privtomc/tomcat/webapps/privatetomcat.readwritebox.com/” So we have opened the directory in right panel of Filezilla.
how to deploy spring application in tomcat

NOTE:If you want to make the application be available at ‘/’ context, the WAR file should be name ‘ROOT.war‘. If you’re uploading a directory with unpacked application, the directory name should be ‘ROOT‘. Otherwise, as in the case for this article naming the WAR file SpringMVCDeploymentDailyRazor.war will result in the application being deployed and be available via the URL like privatetomcat.readwritebox.com/SpringMVCDeploymentDailyRazor or privatetomcat.readwritebox.com/directory_name accordingly. So bear this in mind!

Step 4: Upload your .WAR file or “complete web directory”.

Consequently, please select your .war file in the left panel. Right click and say “Upload”. Please see the below image showing the process to upload SpringMVCDeploymentDailyRazor.war. It will upload your .war to the selected directory.
how to deploy spring application in tomcat

Note: This same method can be used to upload “SpringMVCDeploymentDailyRazor” directory as well.

At any rate, once the .war has been moved to remote server, it gets extracted automatically if you’re using a Private Tomcat server. You can see in the image below SpringMVCDeploymentDailyRazor.war has been extracted to SpringMVCDeploymentDailyRazor. If you’re using a Shared Tomcat server, you need to request Tomcat restart by emailing support@dailyrazor.com
how to deploy spring application in tomcat

Step 5: Check your url after successful deployment.

In conclusion, we have deployed SpringMVCDeploymentDailyRazor.war file. Since our base url is http://PrivateTomcatexamples.dailyrazor.com/ we can find the complete web application at http://PrivateTomcatexamples.dailyrazor.com/SpringMVCDeploymentDailyRazor

Method #2: Copy extracted/unpacked web application (WAR) file using Filezilla.

Another method on how to deploy Spring application in Tomcat is to upload extracted/unpacked war file. This is the recommended method for Shared Tomcat users. Since you will uploading an already extracted war file, you do need not to send a request to support for Tomcat restart.

In this method, the web application is unpacked. As a result, you need to follow all the steps in Method #1 with the exception of Step 4. When you get to Step 4, you have to select complete extracted directory and upload it to the ~/webapps/. directory/folder. Please see the attached image for a visual.
how to deploy spring application in tomcat

Regrettably, this method on how to deploy Spring application in Tomcat might take slightly more time than Method #1. The visual below will give you an idea of what to expect once upload is complete:
how to deploy spring application in tomcat
NOTE: Ultimately, if you want to update changes for the application, you must replace the corresponding files under its document root directory. You need to restart Tomcat Server if the changes are not taking effect.

 

[SPECIAL OFFER]: 15 Days FREE Trial Java Web Hosting

[BENEFITS]:

  • Host Your Spring MVC Web Application for Free!
  • Private Tomcat Server with Dedicated JVM Heap & PermGen
  • Custom Java Development Kit (JDK) version
  • Super Fast Servers with 24/7/365 Technical Support

Click here to access this [SPECIAL OFFER]

 

Method #3: Using Tomcat Manager application.

Incidentally, this method on how to deploy Spring application in Tomcat is the most convenient way to deploy your .war file. It is hassle free. However, it is available for Private Tomcat servers. Unfortunately, it is not available for Shared Tomcat servers. By and large, the reason is because Shared Tomcat server is used multiple customers and cannot support the availability of Tomcat Manager application.

All things considered, with this method you can deploy the web application remotely via a web interface provided by Tomcat. You must have a username and password to access Tomcat’s manager user interface (which is provided to you). You can access the Tomcat Manager by the url given to you in your Tomcat server configuration email. Generally, the Tomcat manager url is formatted as follows: http://yourdomain.com/manager/html

Meanwhile, once you will type your Tomcat Manager url in browser and hit enter. You would be asked to enter your username and password. Please provide your username and password. After that click on OK.
how to deploy spring application in tomcat

After supplying correct username and password, you get into the following screen:
how to deploy spring application in tomcat

From here on, the list of deployed applications is shown at the top, scroll down little bit. You will see the below given deployment screen.
how to deploy spring application in tomcat

As a matter of fact, there are two ways for deploying a web application using the Tomcat Manager application:

  • Deploy directory or WAR file located on server.
  • Upload WAR file to deploy.

The former way is only suitable if the application’s WAR file or directory resides on the server and we know the URL. The latter way is easier in which we can pick up a WAR file and upload it to the server.

Click on “choose file” button. It will open window or MAC directory explorer. Next, navigate to the location of war file. In our case .war file is located at – “D:\learning\spring-web-mvc\SpringMVCDeploymentDailyRazor\target\SpringMVCDeploymentDailyRazor.war “. We have selected the .war file and clicked on Deploy button. For example we select SpringMVCDeploymentDailyRazor.war file. As soon as the WAR file is uploaded to the server, it is unpacked into “/home/privtomc/Tomcat/webapps/PrivateTomcat.readwritebox.com/SpringMVCDeploymentDailyRazor”. Finally, Tomcat Manager adds the newly deployed application to the list of applications:
how to deploy spring application in tomcat

Next we can now access the web application by typing url : http://PrivateTomcatexamples.dailyrazor.com/SpringMVCDeploymentDailyRazor

 

[SPECIAL OFFER]: 15 Days FREE Trial Java Web Hosting

[BENEFITS]:

  • Host Your Spring MVC Web Application for Free!
  • Private Tomcat Server with Dedicated JVM Heap & PermGen
  • Custom Java Development Kit (JDK) version
  • Super Fast Servers with 24/7/365 Technical Support

Click here to access this [SPECIAL OFFER]

 

How to Deploy Spring Application in Tomcat

Some useful information about Spring MVC:

First and foremost, Spring MVC requires these JAR Files to work properly:

  • spring-context-4.3.6.RELEASE.jar
  • spring-beans-4.3.6.RELEASE.jar
  • spring-core-4.3.6.RELEASE.jar
  • commons-logging-1.2.jar
  • spring-expression-4.3.6.RELEASE.jar
  • spring-aop-4.3.6.RELEASE.jar
  • spring-webmvc-4.3.6.RELEASE.jar
  • spring-web-4.3.6.RELEASE.jar
  • jstl-1.2.jar
  • servlet-api-2.5.jar

These JAR libraries must go under web application WEB-INF\lib directory. Deployment would fail in case of absence of any required .jar files.

Sample pom.xml file for Spring MVC:

Next, here is a sample pom.xml to download Spring MVC dependent jars for this how to deploy Spring application in Tomcat tutorial.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>SpringMVCDeploymentDailyRazor</groupId>
	<artifactId>SpringMVCDeploymentDailyRazor</artifactId>
	<version>1.0</version>
	<packaging>war</packaging>
	<name>SpringMVCDeploymentDailyRazor</name>
	<description>Step by step guide to  deploy Spring MVC web application on DailyRazor server</description>

	<properties>
		<jdk.version>1.8</jdk.version>
		<spring.version>4.3.6.RELEASE</spring.version>
		<jstl.version>1.2</jstl.version>
		<servletapi.version>2.5</servletapi.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>${jstl.version}</version>
		</dependency>
		<!-- for compile only, your container should have this -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>${servletapi.version}</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>

	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.5.1</version>
				<configuration>
					<source>${jdk.version}</source>
					<target>${jdk.version}</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.0.0</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Thanks for reading this article on how to deploy Spring application in Tomcat. In case you need any help regarding Spring MVC deployment, please email us at support@dailyrazor.com. We are happy to help you.