This is a personal study notes of Apache Tomcat. Below are main reference material.
- YouTube Apache Tomcat Full Tutorial,owed by Alpha Brains Courses. https://www.youtube.com/watch?v=rElJIPRw5iM&t=801s
When we hit a URL on browser like localhost:8080/example/hello
we are actually reqeusting a resource or service.
Resource refer to a file like .html
,.image
. For example when we hit example root full path which is localhost:8080/example
, we are requesting index.html
and this file will be explained and rendered by browser and showed in a readable way.
I perceive those static files as a kind of resource.
But sometims we only request a service rather than a static file.
For example we click a buttton then browser sends a http request, supposing it’s URL is https://www.system.com/delete?id=1
.
A Tomcat application recieve the request and provide service which is deleting a record in database.
This is requesting for a servcie.
Mapping to a resource is easy. It usually is like access file system.
But mapping to a service is more tricky.
I’m going to show how Tomcat
mapping URL to a resource or service.
Supposing our web application’s root URL is localhost:8080/myweb/
we have a war file like below. I omit some files for readability.
myweb.war
----index.html
----assets
--------hello.img
----WEB-INF
---------web.xml
---------classes
-------------HelloServlet.class
----META-INF
As i mentioned, mapping URL to a resource basically is similar to accessing a file in file system.
For example, If we want to get index.html
, just call localhost:8080/myweb/index.html
.
If wanting hello.img
, just call localhost:8080/myweb/assets/hello.img
.
So it’s very similiar to file system.
Just notice that web root URL localhost:8080/myweb/
represents hello.war
’s first level subdirectory. Then you can get all static resources hierarchyly just as the same way in file system.
Tomcat can have mutiple web applications which are located under webapps
folder.
webapps
----myweb
----myweb1
----myweb2
----ROOT
---------root.txt
What I just showed is how to access static resources in myweb
.
If accessig files in myweb
, we should take localhost:8080/myweb
as root URL.
If accessig files in myweb1
, we should take localhost:8080/myweb1
as root URL.
So myweb
or myweb1
is context which specifies which web apps you want to access.
Besides those web applicaton developed by programmer, Tomcat
always has a folder called ROOT
under webapps
.
If you want to access static resources under ROOT
, root.txt
for example, just remove the context and take localhost:8080/
as the root URL representig the first level subdirectory of ROOT
.
Thus just call localhost:8080/root.txt
then you can get root.txt
.
Mapping URL to service could be more tricky. Because Java servlet classes usually have more deeper file structure, making URL too long.
com.org.servlet.MyServlet.java # It has 4 levels directory in total
acme.personnel.management.MedicalPlan # very long
For readability Tomcat has a configuration file called web.xml
to map to resources with a more simple name.
Supposing we have a servlet called DeleteRecordServlet
whose doGet
method is to delete a record in database, whose location is myweb.war/com/org/servlet/DeleteRecordServlet.java
.
myweb.war # omit some files
----com
--------org
------------servlet
----------------DeleteRecordServlet.java
----META-INF
--------web.xml
--------classes
------------com
----------------org
--------------------servlet
------------------------DeleteRecordServlet.class
Now we need to configure some mapping in web.xml
, mapping a shorter url to DeleteRecordServlet
.
<web-app>
<servlet>
<servlet-name>deleteServletservlet-name>
<servlet-class>com.org.servlet.DeleteRecordServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>deleteServletservlet-name>
<url-pattern>/deleteurl-pattern>
servlet-mapping>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
welcome-file-list>
web-app>
In this way we can hit localhost:8080/myweb/delete
which is more simple than normal url to call DeleteRecordServlet
.