0% found this document useful (0 votes)
13 views

Experiment No 2

The document outlines a practical experiment for installing Java and the Tomcat Server, detailing the components of a web application and the steps for installation. It includes instructions for setting up Tomcat on Windows, macOS, and Ubuntu, configuring server settings, and starting the server. Additionally, it provides guidance on creating a simple 'Hello-world' web application within the Tomcat environment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Experiment No 2

The document outlines a practical experiment for installing Java and the Tomcat Server, detailing the components of a web application and the steps for installation. It includes instructions for setting up Tomcat on Windows, macOS, and Ubuntu, configuring server settings, and starting the server. Additionally, it provides guidance on creating a simple 'Hello-world' web application within the Tomcat environment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Experiment No.

02
Aim: Study practical on installation of java, Tomcat Server

Theory:

1.1 Web Application (Webapp)


A web application (or webapp), unlike standalone application, runs over the Internet.
Examples of webapps are google, amazon, facebook and twitter.
A webapp is typically a 3-tier (or multi-tier) client-server database application run over the
Internet as illustrated in the diagram below. It comprises five components:
1. HTTP Server: E.g., Apache HTTP Server, Apache Tomcat Server, Microsoft
Internet Information Server (IIS), nginx, Google Web Server (GWS), and others.
2. HTTP Client (or Web Browser): E.g., Internet Explorer (MSIE), FireFox,
Chrome, Safari, and others.
3. Database: E.g., Open-source MySQL, PostgreSQL, Apache Derby, mSQL, SQLite,
OpenOffice's Base; Commercial Oracle, IBM DB2, SAP SyBase, Microsoft SQL
Server, Microsoft Access; and others.
4. Client-Side Programs: Could be written in HTML Form, JavaScript, and others.
5. Server-Side Programs: Could be written in Java Servlet/JSP, ASP, PHP, Perl,
Python, JavaScript, and others.

A typical use case is:


1. (client-to-server) A user, via a web browser (HTTP client), issues a URL request to an
HTTP server to start a webapp.
2. (server-to-client) The HTTP server returns an HTML form (client-side program),
which is loaded and rendered in the client's browser.
3. (client-to-server) The user fills up the query criteria inside the form and submits the
form. This sends the query parameters to a server-side program.
4. (server-to-client) The server-side program receives the query parameters, queries the
database based on these parameters, and returns the query result to the client-side
program.
5. (client) The client-side program receives the query result and displays on the browser.
6. The process repeats for the next request-response.
1.2 Hypertext Transfer Protocol (HTTP)
 HTTP is an application layer protocol runs over TCP/IP. The IP provides support for
routing and addressing (via a unique IP address for machines connected to the Internet);
while TCP supports multiplexing via 64K ports from port number 0 to 65535. The
default port number assigned to HTTP is TCP port 80. (Notes: TCP Port numbers below
1024 are reserved for popular protocols such as HTTP, FTP, SMTP; Port numbers 1024
and above could be used for applications.)
 HTTP is an asynchronous request-response application-layer protocol. A client sends a
request message to the server. The server then returns a response message to the client.
In other words, HTTP is a pull protocol, a client pulls a page from the server (instead of
server pushes pages to the clients).
 The syntax of the message is defined in the HTTP specification.

1.3 Apache Tomcat HTTP Server


Apache Tomcat is a Java-capable HTTP server, which could execute special Java programs
known as "Java Servlet" and "Java Server Pages (JSP)". Tomcat is an open-source project,
under the "Apache Software Foundation" (which also provides the most use, open-source,
industrial-strength Apache HTTP Server). The mother site for Tomcat
is http://tomcat.apache.org. Alternatively, you can find tomcat via the Apache mother site
@ http://www.apache.org.
Tomcat was originally written by James Duncan Davison (then working in Sun Microsystem)
in 1998, based on an earlier Sun's server called Java Web Server (JWS). It began at version
3.0 after JWS 2.1 it replaced. Sun subsequently made Tomcat open-source and gave it to
Apache.
The various Tomcat releases are:
1. Tomcat 3.0 (1999): Reference Implementation (RI) for Servlet 2.2 and JSP 1.1.
2. Tomcat 4.1 (Sep 2002): RI for Servlet 2.3 and JSP 1.2.
3. Tomcat 5.0 (Dec 2003): RI for Servlet 2.4 and JSP 2.0.
4. Tomcat 6.0 (Feb 2007): RI for Servlet 2.5 and JSP 2.1.
5. Tomcat 7.0 (Jan 2011): RI for Servlet 3.0, JSP 2.2 and EL 2.2.
6. Tomcat 8.0 (Jun 2014): RI for Servlet 3.1, JSP 2.3, EL 3.0 and WebSocket 1.0. Tomcat
8.5 (June 2016) supports HTTP/2, OpenSSL, TLS virtual hosting and JASPIC 1.1.
7. Tomcat 9.0 (Jan 2018): RI for Servlet 4.0, JSP 2.3, EL 3.0, WebSocket 1.0, JASPIC
1.1.
8. Tomcat 10.0 (Feb 2021): Support Servlet 5.0, JSP 3.0, EL 4.0, WebSocket 2.0 and
Authentication 2.0.
2. How to Install Tomcat and Get Started with Java Servlet Programming
2.1 STEP 0: Create a Directory to Keep all your Works
I shall assume that you have created a directory called "c:\myWebProject" (for Windows) or
"~\myWebProject" (for macOS) in your earlier exercises. Do it otherwise. This step is
important; otherwise, you will be out-of-sync with this article and will not be able to find
your files later.
2.2 STEP 1: Download and Install Tomcat
For Windows
1. Goto http://tomcat.apache.org ⇒ Under "Tomcat 10.0.{xx} Released",
where {xx} is the latest update number ⇒ Click "Download" ⇒ Under
"10.0.{xx}" ⇒ Binary Distributions ⇒ Core ⇒ "zip" (e.g., "apache-tomcat-
10.0.{xx}.zip", about 12 MB).
2. UNZIP (right-click ⇒ Extract All) the downloaded file into your project
directory "c:\myWebProject". Tomcat shall be unzipped into directory
"c:\myWebProject\apache-tomcat-10.0.{xx}".
3. For EASE OF USE, we shall shorten and RENAME this directory to
"c:\myWebProject\tomcat".
Take note of Your Tomcat Installed Directory. Hereafter, I shall refer to the Tomcat
installed directory as <TOMCAT_HOME>.
For macOS
1. Goto http://tomcat.apache.org ⇒ Under "Tomcat 10.0.{xx} Released",
where {xx} is the latest update number ⇒ Click "Download" ⇒ Under
"10.0.{xx}"⇒ Binary distribution ⇒ Core ⇒ "tar.gz" (e.g., "apache-tomcat-
10.0.{xx}.tar.gz", about 12 MB).
2. To install Tomcat:
a. Double-click the downloaded tarball (e.g., "apache-tomcat-
10.0.{xx}.tar.gz") to expand it into a folder (e.g., "apache-tomcat-
10.0.{xx}").
b. Move the extracted folder (e.g., "apache-tomcat-10.0.{xx}") to your
project directory "~/myWebProject".
c. For EASE OF USE, we shall shorten and RENAME this folder to
"tomcat", i.e., "~/myWebProject/tomcat".
Take note of Your Tomcat Installed Directory. Hereafter, I shall refer to the Tomcat
installed directory as <TOMCAT_HOME>.
For Ubuntu
Read "How to Install Tomcat on Ubuntu". You need to switch between these two articles.
For academic learning, I recommend "zip" (or "tar.gz") package, as you could simply delete
the entire directory when Tomcat is no longer needed (without running any un-installer). You
are free to move or rename the Tomcat's installed directory. You can install (unzip) multiple
copies of Tomcat in the same machine.
Tomcat's Sub-Directories
Take a quick look at the Tomcat installed directory. It contains the these sub-directories:
 bin: contains the binaries and scripts (e.g., startup.bat and shutdown.bat for
Windows; startup.sh and shutdown.sh for Unixes and macOS).
 conf: contains the system-wide configuration files, such as server.xml, web.xml,
and context.xml.
 webapps: contains the webapps to be deployed. You can also place the WAR (Webapp
Archive) file for deployment here.
 lib: contains the Tomcat's system-wide library JAR files, accessible by all webapps. You
could also place external JAR file (such as MySQL JDBC Driver) here.
 logs: contains Tomcat's log files. You may need to check for error messages here.
 work: Tomcat's working directory used by JSP, for JSP-to-Servlet conversion.
2.3 STEP 2: Create an Environment Variable JAVA_HOME
(For Windows)
You need to create an environment variable (system variable available to all applications)
called "JAVA_HOME", and set it to your JDK installed directory.
Follow the steps HERE!
(For macOS)
Skip this step. No need to do anything.
2.4 STEP 3: Configure the Tomcat Server
The Tomcat configuration files, in XML format, are located in the "conf" sub-directory of
your Tomcat installed directory, e.g. "c:\myWebProject\tomcat\conf" (for Windows) or
"~/myWebProject/tomcat/conf" (for macOS). The important configuration files are:
1. server.xml
2. web.xml
3. context.xml
Make a BACKUP of the configuration files before you proceed!!!
Step 3(a) "conf\server.xml" - Set the TCP Port Number
Use a programming text editor (e.g., Sublime Text, Atom) to open the configuration file
"server.xml".
The default TCP port number configured in Tomcat is 8080, you may choose any number
between 1024 and 65535, which is not used by existing applications. We shall choose 9999 in
this article. (For production server, you should use port 80, which is pre-assigned to HTTP
server as the default port number.)
Locate the following lines (around Line 68) that define the HTTP connector, and
change port="8080" to port="9999".
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="9999" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
Step 3(b) "conf\web.xml" - Enable Directory Listing
Again, use a programming text editor to open the configuration file "web.xml".
We shall enable directory listing by changing "listings" from "false" to "true" for the
"default" servlet. This is handy for test system, but not for production system for security.
Locate the following lines (around Line 122) that define the "default" servlet; and change the
"listings" from "false" to "true".
<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>
Step 3(c) "conf\context.xml" - Enabling Automatic Reload
We shall add the attribute reloadable="true" to the <Context> element to enable automatic
reload after code changes. Again, this is handy for test system but not recommended for
production, due to the overhead of detecting changes.
Locate the <Context> start element (around Line 19), and change it to:
<Context reloadable="true" crossContext="true" parallelAnnotationScanning="true">
......
......
</Context>
2.5 STEP 4: Start Tomcat Server
The Tomcat's executable programs and scripts are kept in the "bin" sub-directory of the
Tomcat installed directory.
Step 4(a) Start Server
For Windows
I shall assume that Tomcat is installed in "c:\myWebProject\tomcat". Launch a CMD shell
and issue:
c: // Change drive
cd \myWebProject\tomcat\bin // Change directory to your Tomcat's binary directory
startup // Run startup.bat to start tomcat server
For macOS
I assume that Tomcat is installed in "~/myWebProject/tomcat". To start the Tomcat server,
open a new "Terminal" and issue:
cd ~/myWebProject/tomcat/bin // Change directory to your Tomcat's binary directory
./catalina.sh run // Run catalina.sh to start tomcat server
A new Tomcat console window appears (with Java's coffee-cup logo as icon). Study the
messages on the console. Look out for the Tomcat's port number. Double check that Tomcat
is running on port 9999 as configured.
Error messages will be sent to this console. System.out.println() issued by your Java servlets
will also be sent to this console.
............
............
xxxxx INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler
["http-nio-9999"]
xxxxx INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-
nio-8009"]
xxxxx INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [1325] ms
(Skip Unless ...) Cannot Start Tomcat: Read "How to Debug".
Step 4(b) Start a Client to Access the Server
Start a browser (Firefox, Chrome) as an HTTP client. Issue URL "http://localhost:9999" to
access the Tomcat server's welcome page. The hostname "localhost" (with IP address
of 127.0.0.1) is meant for local loop-back testing within the same machine. For users on the
other machines over the net, they have to use the server's IP address or DNS domain name in
the form of "http://serverHostnameOrIPAddress:9999".

(Optional) Try issuing URL http://localhost:9999/examples to view the servlet and JSP
examples. Try running some of the servlet examples.
Step 4(c) Shutdown Server
For Windows
You can shutdown the tomcat server by either:
1. Press Ctrl-C on the Tomcat console; OR
2. Run "<TOMCAT_HOME>\bin\shutdown.bat" script. Open a new "cmd" and
issue:
3. c: // Change the current drive
4. cd \myWebProject\tomcat\bin // Change directory to your Tomcat's binary
directory

shutdown // Run shutdown.bat to shutdown the server


For macOS
To shutdown the Tomcat server:
1. Press Control-C (NOT Command-C) on the Tomcat console; OR
2. Run the "<TOMCAT_HOME>/bin/shutdown.sh" script. Open a new "Terminal"
and issue:
3. cd ~/myWebProject/tomcat/bin // Change current directory to Tomcat's bin
directory

./shutdown.sh // Run shutdown.sh to shutdown the server


WARNING: You MUST properly shutdown the Tomcat. DO NOT kill the CAT by pushing
the window's "CLOSE" button.
2.6 STEP 5: Develop and Deploy a "Hello-world" WebApp
Step 5(a) Create the Directory Structure for your WebApp

Let's call our first webapp "hello". Goto Tomcat's "webapps" sub-directory and create the
following directory structure for your webapp "hello" (as illustrated). The directory names
are case-sensitive!!
1. Under Tomcat's "webapps", create your webapp's root directory "hello" (i.e.,
"<TOMCAT_HOME>\webapps\hello").
2. Under "hello", create a sub-directory "WEB-INF" (case sensitive, a "dash" not an
underscore) (i.e., "<TOMCAT_HOME>\webapps\hello\WEB-INF").
3. Under "WEB-INF", create a sub-sub-directory "classes" (case sensitive, plural) (i.e.,
"<TOMCAT_HOME>\webapps\hello\WEB-INF\classes").
You need to keep your web resources (e.g., HTMLs, CSSs, images, scripts, servlets, JSPs) in
the proper directories:
 "hello": The is called the context root (or document base directory) of your webapp. You
should keep all your HTML files and resources visible to the web users (e.g., HTMLs,
CSSs, images, scripts, JSPs) under this context root.
 "hello/WEB-INF": This directory, although under the context root, is not visible to the
web users. This is where you keep your application's web descriptor file "web.xml".
 "hello/WEB-INF/classes": This is where you keep all the Java classes such as servlet
class-files.
You need to RE-START your Tomcat server to pick up the hello webapp. Check the
Tomcat's console to confirm that "hello" application has been properly deployed:
......
xxxxx INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory
Deploying web application directory [xxx\webapps\hello]
xxxxx INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory
Deployment of web application directory [xxx\webapps\hello] has finished in [38] ms
......
You can issue the following URL to access the web application "hello":
http://localhost:9999/hello
You should see the directory listing of the directory "<TOMCAT_HOME>\webapps\hello",
which shall be empty at this point of time. Take note that we have earlier enabled directory
listing in "web.xml". Otherwise, you will get an error "404 Not Found".
Step 5(b) Write a Welcome Page
Create the following HTML page and save as "HelloHome.html" in your webapp's root
directory "hello".
1 <!DOCTYPE html>
2 <html>
3 <head><title>My Home Page</title></head>
4 <body>
5 <h1>Hello, world!</h1>
6 <p>My Name is so and so. This is my HOME.</p>
7 </body>
8 </html>

You can browse this page by issuing this URL:

http://localhost:9999/hello/HelloHome.html

Alternatively, you can issue an URL to your webapp's root "hello":


http://localhost:9999/hello

The server will return the directory listing of your base directory. You can then click on
"HelloHome.html".
Rename "HelloHome.html" to "index.html", and issue a directory request again:
http://localhost:9999/hello

Now, the server will redirect the directory request to "index.html", if the root directory
contains an "index.html", instead of serving the directory listing.
Rename "index.html" back to "HelloHome.html", so that you could see get directory listing
for convenience.
You can check out the home page of your peers by issuing:

http://YourPeerHostnameOrIPAddr:9999/hello
http://YourPeerHostnameOrIPAddr:9999/hello/HelloHome.html
with a valid "YourPeerHostnameOrIPAddr", provided that your peer has started his tomcat
server and his firewall (and the network) does not block your access. You can use command
such as "ipconfig" (Windows), "ifconfig" (macOS and Unix) to find your IP address.

Conclusion : In this way we have studied practical on installation of java, Tomcat


Server.

You might also like