Ultimate Guide to Hosting Java Web Application on Tomcat Server
Options for web hosting Java web application are not readily available as those for PHP-based web applications. Besides, one common challenge that Java web developers face after developing a Java website on their local computer is how to deploy it to a Java web hosting environment.
In this article, we’ll discuss web hosting Java web application with clear instructions on how to host a JSP website or Servlet website on a Tomcat servlet container. Then various ways you can deploy your Java web application to a Tomcat hosting environment using FTP and Tomcat Manager will be discussed. Also, we’ll also cover hosting Java web application with MySQL for those web applications that need to establish JDBC connections to a MySQL database.
Hosting Java Web Application
Java web applications are often developed using programming technologies such as Java Servlets, JavaServer Pages (JSP) or Java Beans. Also, some people opt to work within Java web frameworks such as Spring, Spring MVC, Spring Boot, Sruts, JSF (Java Server Faces), MyFaces, Hibernate and many others.
Nevertheless, whatever development approach you take in developing your Java website, you’ll need to find a suitable Java hosting environment to make it available to its intended users on the Internet.
Apache Tomcat is by far the most popular open-source Server container for hosting Java web applications. It’s lightweight, fast, secure, portable and supports all Java SE and most Java EE specifications. Therefore, if your Java web application is not using Enterprise Java Beans and other enterprise features of Java EE, then you can conveniently host it on a Tomcat server.
Hosting for Java Web Application on Tomcat
In the first place, make sure that your web application is packaged as a WAR file. Below are the steps on setting up hosting for Java web application on Tomcat:
If you’re deploying your website using an FTP client:
1) Upload your application web archive file (.war file) or directory with unpacked application into “/home/username/tomcat/webapps/domainname/” directory. The “/username/” is provided by your web hosting provider and “/domain/” is the domain name your web hosting is set up for.
2) If you want to make the application available at ‘/’ context, the WAR file should be named ‘ROOT.war.’ On the other hand, if you’re uploading a directory with an unpacked application, then the directory should be named ‘ROOT’.
If this convention is not followed, the application will be deployed and be available via URL like “domainname/war_file_name” or “domainname/directory_name” accordingly.
3) Usually, Tomcat automatically tracks changes, and deploys new applications, undeploys removed applications and applies changes into running applications.
However, if the changes aren’t applied automatically within 10 minutes, please restart Tomcat via SSH or via the web interface to apply the changes and deploy your application manually.
via Tomcat Manager:
Next, login to Tomcat Manager (the web host will provide you with the URL). However, if you have any troubles with login credentials for Tomcat Manager, you may find correct login info for Tomcat Manager application in the “/home/username/tomcat/conf/tomcat-users.xml” file.
When logged in, take the following steps:
A. If you have already uploaded the web application onto the server:
1) Scroll down the page to the section called “Deploy directory or WAR file located on the server.” Enter the context name preceded by “/” (e.g., “/myapp”) in the “Context Path” field.
2) Leave the “XML Configuration file URL” field blank.
3) Locate the WAR file and enter its absolute name (e.g., “/absolute/path/ROOT.war”) in the “WAR or Directory URL” field.
4) Next, click the “Deploy” button. The page will reload.
5) At the top of the page, verify that the message displayed is “OK – Deployed application at context path /myapp”.
In the “Applications” section check that there is a line with the new application. The application is now ready and available at the specified URL.
B. If application is on your local PC:
Furthermore, the technique described above section works if you are able to copy the WAR file to the server’s file system using FTP or the File Manager.
However, if there is no way to do that for some reason, you can still deploy an application using a “remote deployment” feature. Please follow these steps to do this:
1) Locate the WAR file on your local machine and rename it to the context name with “war” extension, e.g., myapp.war or to ROOT.war (if ROOT name specified, the application will be accessible at default context “/”)
2) Access the Tomcat Web Application Manager and scroll down to the section called “WAR file to deploy”.
3) Click on the “Browse…” button and select the needed .war file on your local machine.
4) Click “Deploy”. The browser will upload the selected .war file to the server and Tomcat will deploy it afterward.
Hosting Java Web Application with MySQL
If you’re developing a database driven web application in Java, then there is a need for hosting Java web application with MySQL. MySQL is the most popular open-source database management system out there. However, you can host a Java web application with just about any database system such as PostgreSQL, MS SQL, SQLite as well as others.
Tomcat uses Java Database Connectivity (JDBC) for handling database connections and there’s an already developed driver by MySQL called Connector/J for this purpose. Visit this website for a good tutorial on hosting Java web application with MySQL.
While the available options for hosting Java web application are not as vast as its PHP or Python counterpart, there is some quality Java hosting solutions that are cost-effective and suitable for small to medium-sized Java web application.
If you’re in the marketing for a web hosting environment to host your Java web website, do check out our affordable Java hosting options. And don’t hesitate to contact us at firstname.lastname@example.org if you have any questions or need further assistance.