Quick Guide to Setup Java Servlet on an Apache Tomcat 8 (on Windows)
In this article we will understand how to setup a Java servlet on an Apache Tomacat 8 on Windows.
Note: In my below example I have used some hard coded value for simplicity for viewer to follow and can be changed based on the need.
Step 1: Download Tomcat 8
Go to the URL https://tomcat.apache.org/download-80.cgi and download the zipped package to any of your choice directory, let’s say “C:\JavaSample\bin”.
Once you download the zip file, you can extract into the same location “C:\ JavaSample\bin\apache-tomcat-8.0.28\apache-tomcat-8.0.28" which I would refer this as my Tomcat Installation directory called as “TOMCAT_HOME” or CATALINA_HOME.
Catalina is codename for Tomcat 5 and above.
Step 2: Download and Install Java Development Kit
Go to URL https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html and download Java development kit 8 for windows based on the bitness of the machine.
Step 3: Creating Environmental Variables for Windows
- Create an Environmental Variable called CATALINA_HOME with value as your Tomcat installation directory. In my example I have created a server variable CATALINA_HOME= C:\JavaSample\bin\apache-tomcat-8.0.28\apache-tomcat-8.0.28.
To confirm open command prompt and check if it has been set correctly.
- Take a note of location where your Java Development(JDK) is installed and create an environmental variable called “JAVA_HOME” and set the value to the JDK installed directory. In my example I have set JAVA_HOME=”C:\Program Files\Java\jdk1.8.0_65".
To confirm open command prompt and check if it has been set correctly.
Step 3: Configure Apache Tomcat Server
There are few configuration changes required to setup Tomcat server and in our case we will be changing below files under %CATALINA_HOME%\Conf:
- Server.xml
- Web.xml
- Context.xml
Open server.xml under %CATALINA_HOME%\conf and change the settings as high lightened in yellow:
<Connector port="9999" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
|
Open web.xml under %CATALINA_HOME%\conf and change the settings as high lightened in yellow:
<servlet> <servlet-name>default</servlet-name> <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>listings</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
|
Open context.xml under %CATALINA_HOME%\conf and change the settings as high lightened in yellow:
<Context reloadable="true">
|
Step 4: Deploy a Java Servlet
In my case, I have used one of the open source Java Servlet and download the code from https://pebble.sourceforge.net/ and copy the pebble.war from the source code to %CATALINA_HOME%\webapps.
Step 5: Start the Tomcat Server
Launch the command prompt in admin mode and navigate to %CATALINA_HOME%\Bin folder and run below command:
Startup.bat
|
Now open a browser and browse https://localhost:9999 which should show the default Tomcat server's welcome page and then browse https://localhost:9999/pebble which should show the website which is deployed under webapps.
You can shut down the Tomcat server by running below command from command prompt by navigating to %CATALINA_HOME%\Bin
Shutdown.bat
|
References:
https://www.ntu.edu.sg/home/ehchua/programming/howto/Tomcat_HowTo.html