Experiment No 2
Experiment No 2
02
Aim: Study practical on installation of java, Tomcat Server
Theory:
(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
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>
http://localhost:9999/hello/HelloHome.html
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.