Tomcat Notes: URL Mapping

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



1、URL Mapping To Resources

1.1、What we request for when hitting a URL

When we hit a URL on browser like localhost:8080/example/hellowe 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.htmland 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.

1.2、Mapping Code

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 Tomcatmapping 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

1.2.1、Mapping To Resources

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.

1.2.1.1、Access Resourses in ROOT

Tomcat can have mutiple web applications which are located under webappsfolder.

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/mywebas root URL.
If accessig files in myweb1, we should take localhost:8080/myweb1as root URL.

So mywebor myweb1is context which specifies which web apps you want to access.

Besides those web applicaton developed by programmer, Tomcatalways has a folder called ROOTunder webapps.

If you want to access static resources under ROOT, root.txtfor 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.txtthen you can get root.txt.



1.2.2、Mapping To Service

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.xmlto map to resources with a more simple name.

Supposing we have a servlet called DeleteRecordServletwhose doGetmethod 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.

<?xml version = "1.0" encoding = "ISO-8859-1"?>
<web-app>
    <servlet>
        <servlet-name>deleteServlet</servlet-name>
        <servlet-class>com.org.servlet.DeleteRecordServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>deleteServlet</servlet-name>
        <url-pattern>/delete</url-pattern>
    </servlet-mapping>
    
    <welcome-file-list>
      <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

In this way we can hit localhost:8080/myweb/deletewhich is more simple than normal url to call DeleteRecordServlet.

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-18 05:18:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-18 05:18:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-18 05:18:01       87 阅读
  4. Python语言-面向对象

    2024-01-18 05:18:01       96 阅读

热门阅读

  1. 【Flutter】关注的那些点

    2024-01-18 05:18:01       62 阅读
  2. 前端基础:回顾es6相关知识

    2024-01-18 05:18:01       61 阅读
  3. http获取用户访问的IP地址

    2024-01-18 05:18:01       42 阅读
  4. 使用 rinetd 搭建简单端口重定向服务

    2024-01-18 05:18:01       64 阅读
  5. Linux内核--网络协议栈(三)UDP协议层/IP层的处理

    2024-01-18 05:18:01       52 阅读
  6. 【数据结构】链表

    2024-01-18 05:18:01       54 阅读
  7. Qt中的线程池

    2024-01-18 05:18:01       53 阅读
  8. 三子棋/井字棋(C语言)

    2024-01-18 05:18:01       42 阅读
  9. 深入探讨 Go 语言中的 Map 类型(续)

    2024-01-18 05:18:01       59 阅读
  10. Linux 文件搜索大师:掌握 find 命令的艺术与示例

    2024-01-18 05:18:01       46 阅读
  11. Qt:信号

    2024-01-18 05:18:01       52 阅读
  12. Matlab中常见的数据平滑方式

    2024-01-18 05:18:01       47 阅读
  13. Lumerical ------ 直波导仿真及技巧

    2024-01-18 05:18:01       57 阅读
  14. HTTP API 认证技术详解(一):Basic Authentication

    2024-01-18 05:18:01       53 阅读