Apache Tomcat 9 Tutorial for Beginners 1


This article is all about Apache Tomcat 9. We will cover installation and configuration of Apache Tomcat 9.

Apache Tomcat is an open source web/application server. $CATALINA_HOME is the the root of Tomcat installation or Tomcate directory to be more specific.

Apache Tomcat 9 Installation

Download Tomcat 9 from http://tomcat.apache.org/download-90.cgi#9.0.0.M4 

Note: Tomcat 9.0 requires Java SE 8 or later.  (Download Java8 from go to http://www.oracle.com/technetwork/java/javase/downloads/index.html)
Extract the Zip File.
These are some of the key tomcat directories:
/bin – The *.sh files and the *.bat files can be used for Startup shutdown of Tomcat.
/conf – It contains files to configure tomcat and we will see it in later part of this article. Note: tomcat-users.xml and server.xml
/lib – All required jar files (JSP, Servlet, etc.) are available here.
/logs – All Log files are stored here by default.
/webapps – It contains all applications. We need to put application here to deploy it in Tomcat.
54.1 Tomcat Installation Directory

 

Open Command Prompt in Windows or Terminal in Linux and run startup.bat or startup.sh based on the operating system you are using.

First of all verify Java installation with the java -version command.

Go to $CATALINA_HOMEbin and run startup.bat or startup.sh based on your operating system.

If Tomcat is not running even though you have installed Java  8 then verify JAVA_HOME

C:apache-tomcat-9.0.0.M4bin>echo %java_home%

C:Program FilesJavajdk1.7.0_21

Let’s change the JAVA_HOME environment variable first to troubleshoot.

To change the JAVA_HOME environment variable visit our earlier post: Configuring JAVA_HOME in Windows, Linux , MAC OS 

Verify JAVA_HOME

C:apache-tomcat-9.0.0.M4bin>echo %java_home%

C:Program FilesJavajdk1.8.0_25

Now try to execute startup.bat (I am using Windows Operating system here)

54.2. Run Tomcat 9 Server

Bingo!

Let’s verify in browser: http://localhost:8080/

54.3. Run Apache Tomcat 9 in Browser

Change Tomcat Default Port

Go to $CATALINA_HOME/conf and open server.xml file in editor.
Find Connector Tag:

<Connector port=”8080″ protocol=”HTTP/1.1″ connectionTimeout=”20000″      redirectPort=”8443″ />

54.4. Apache Tomcat 9 Server.xml File

Change the port number.
<Connector port=”9999″ protocol=”HTTP/1.1″ connectionTimeout=”20000″      redirectPort=”8443″ />

Go to $CATALINA_HOME/bin and run startup.bat or startup.sh based on your operating system.

Now we won’t be able to open Tomcat on 8080 port as we have changed it to 9999.

54.5. Apache Tomcat 9 Default Port 8080 changed

Let’s verify in browser: http://localhost:9999/

54.6. Apache Tomcat 9 Default Port 8080 changed to 999

Bingo!!!

How to change the ROOT application

First of all Download Source code from https://github.com/spring-projects/spring-petclinic. Create a WAR file and copy it in the $CATALINA_HOME/webapps folder.

Go to $CATALINA_HOMEconf and open server.xml file in editor.

Find Host element from the XML file.

Before:

<Host name=”localhost”  appBase=”webapps” unpackWARs=”true” autoDeploy=”false”>

<!– SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html –>
<!–
<Valve className=”org.apache.catalina.authenticator.SingleSignOn” />
–>
<!– Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern=”common” –>
<Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs”
prefix=”localhost_access_log” suffix=”.txt”
pattern=”%h %l %u %t &quot;%r&quot; %s %b” />
</Host>

Configure Context root. put the context name in the docBase and add other details as shown below. Keep the autoDeploy value as False.

After:

<Host name=”localhost”  appBase=”webapps” unpackWARs=”true” autoDeploy=”false”>

<!– SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html –>
<!–
<Valve className=”org.apache.catalina.authenticator.SingleSignOn” />
–>
<Context path=”” docBase=”petclinic” debug=”0″ reloadable=”true”>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>

<!– Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern=”common” –>
<Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs”
prefix=”localhost_access_log” suffix=”.txt”
pattern=”%h %l %u %t &quot;%r&quot; %s %b” />
</Host>

Restart the Tomcat Server. Let’s verify in browser: http://localhost:9999/

54.7. Root application changed in Apache Tomcat 9

Bingo!!!

Access Tomcat Manager App

When we use http://localhost:9999/and try to click on Manager App, it asks for authentication.

54.9. Apache Tomcat 9 Manager App Authentication

Go to $CATALINA_HOMEconf and open tomcat-users.xml

Add following  statements:

<role rolename=”manager-gui”/>
<role rolename=”manager-script”/>

<user username=”admin” password=”password” roles=”manager-gui,manager-script” />

Restart the Tomcat Server. Let’s verify in browser: http://localhost:9999/and try to click on Manager App, it asks for authentication. Give User name and Password.

54.9. Apache Tomcat 9 Manager App Authentication

Now verify Tomcat Web Application Manager

54.10. Tomcat Web Application Manager

We can Start, Stop, Reload, Undeploy application from Tomcat Web Application Manager.

Bingo!!!

Leave a comment

Your email address will not be published. Required fields are marked *