Tomcat Notes: URL Mapping

这篇具有很好参考价值的文章主要介绍了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.文章来源地址https://www.toymoban.com/news/detail-802607.html

到了这里,关于Tomcat Notes: URL Mapping的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • TOMCAT部署及优化(Tomcat配置文件参数优化,Java虚拟机(JVM)调优)

    TOMCAT tomcat :是一个开放源代码的web应用服务器,基于java代码开发的。也可以理解为tomacat就是处理动态请求和基于java代码的页面开发。可以在html当中写入java代码,tomcat可以解析html页面当中的java,执行动态请求,动态页面。 tomcat是机制存在一些问题,如果不对tomcat进行优化

    2024年02月13日
    浏览(21)
  • Tomcat:Java Web

    简介 Apache Tomcat 是 Java Web 应用程序开发中最为常用的服务器之一。作为一个开源、轻量级的 Servlet 容器和 JSP 容器,Tomcat 提供了一个稳定可靠的运行环境,使得开发者可以快速开发、部署和管理 Java Web 应用程序。本文将深入介绍 Tomcat 的特点、优势、安装、配置和基本用法,

    2024年03月23日
    浏览(33)
  • java:Tomcat

    在讲 Tomcat 是啥之前,我们先来了解一些概念。 服务器 可以理解为一个高性能的电脑,但是这个电脑现在什么软件都没有安装。 web 服务器 给这个服务器安装一些服务器软件,如 nginx、Apache、Tomcat 等。这台服务器就可以帮我们接收用户的请求,处理请求,做出响应。 Tomcat

    2024年02月11日
    浏览(30)
  • JAVA配置tomcat

    IDEA新建module的时候没有javaee的选项,这时候可以先选择普通的java项目进行创建。 图片中的a就是新建的java module。 右键a文件夹,选择Add Framework Support。 然后选中Web Application,点击右下角的ok即可。 a文件夹下出现带小蓝的web就说明成功了。 关联相关包 将jar包拷到libs文件夹下

    2023年04月08日
    浏览(29)
  • Java——《面试题——tomcat篇》

    全文章节 Java——《面试题——基础篇》 Java——《面试题——JVM篇》 Java——《面试题——多线程并发篇》 Java——《面试题——Spring篇》 Java——《面试题——SpringBoot篇》 Java——《面试题——MySQL篇》​​​​​​ Java——《面试题——SpringCloud》 Java——《面试题——Dob

    2024年02月12日
    浏览(29)
  • Java项目部署到tomcat启动

    修改Tomcat的Context设置 path: 指定访问该Web应用的URL入口。这里可为 path=“/“或path=”” docBase: docBase=“/projectName” 或绝对路径:docBase=“D:tomcatwebappsprojectName” reloadable: 如果这个属性设为true,tomcat服务器在运行状态下会监视在WEB-INF/classes和WEB-INF/lib目录下class文件的改动

    2024年02月10日
    浏览(34)
  • java:Http协议和Tomcat

    Hyper Text Transfer Protocol 超文本传输协议,规定了浏览器和服务器之间数据传输的规则 特点: 基于TCP协议,面向连接,安全 基于请求响应模型:一次请求对应一次响应 HTTP协议是无状态协议,对事务的处理没有记忆能力,每次请求-响应都是独立的. 优点 速度较快 缺点 多次请求间无法共

    2024年04月26日
    浏览(18)
  • java-初识Servlet,Tomcat,JDBC

    java入门须知的重要概念/名词/技术 等 Servlet是Java Web开发中的一个核心组件,它是 基于Java语言编写的服务器端程序,可以接收Web容器(如Tomcat)发送过来的HTTP请求 ,并向客户端发送HTTP响应。Servlet通常用来处理动态Web页面、Web表单数据、管理会话(session)等任务。 实际上,

    2024年02月11日
    浏览(29)
  • Java Web Tomcat 23.7.5

    1.1 简介 1.1.1 什么是Web服务器 Web服务器是一个应用程序( 软件 ),对HTTP协议的操作进行封装,使得程序员不必直接对协议进行操作,让Web开发更加便捷。主要功能是\\\"提供网上信息浏览服务\\\"。 Web服务器是安装在服务器端的一款软件,将来我们把自己写的Web项目部署到Web To

    2024年02月13日
    浏览(30)
  • SpringBoot项目配置Eureka时,内嵌TomCat无法启动java.lang.IllegalStateException: StandardEngine[Tomcat].Standa

    java.lang.IllegalStateException: StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[] failed to start SpringBoot项目可以正常启动,但是一添加@EnableEurekaServer相关注解再启动SpringBoot就报如上错误。我是因为有两个jdk版本,一个8一个17. 从根本原因可以看出是SpringBoot的内嵌Tomcat报错,这是

    2024年02月09日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包