首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

从零手写实现 apache Tomcat-02-web.xml 入门详细介绍

创作缘由

平时使用 tomcat 等 web 服务器不可谓不多,但是一直一知半解。

于是想着自己实现一个简单版本,学习一下 tomcat 的精髓。

系列教程

从零手写实现 apache Tomcat-01-入门介绍[1]

从零手写实现 apache Tomcat-02-web.xml 入门详细介绍[2]

从零手写实现 tomcat-03-基本的 socket 实现[3]

从零手写实现 tomcat-04-请求和响应的抽象[4]

从零手写实现 tomcat-05-servlet 处理支持[5]

从零手写实现 tomcat-06-servlet bio/thread/nio/netty 池化处理[6]

从零手写实现 tomcat-07-war 如何解析处理三方的 war 包?[7]

从零手写实现 tomcat-08-tomcat 如何与 springboot 集成?[8]

从零手写实现 tomcat-09-servlet 处理类[9]

从零手写实现 tomcat-10-static resource 静态资源文件[10]

从零手写实现 tomcat-11-filter 过滤器[11]

从零手写实现 tomcat-12-listener 监听器[12]

web.xml

web 应用,最核心的文件大概就是 web.xml 了。

在Java Web应用中,web.xml是一个非常重要的配置文件,它告诉服务器你的应用是怎么工作的,就像一个说明书一样。

例子

一般如下:

<!-- servlet 配置 --> <servlet> <servlet-name>my</servlet-name> <servlet-class>com.github.houbb.minicat.support.servlet.MyMiniCatHttpServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>my</servlet-name> <url-pattern>/my</url-pattern> </servlet-mapping>

<!-- Filter 配置 --> <filter> <filter-name>LoggingFilter</filter-name> <filter-class>com.github.houbb.minicat.support.filter.MyMiniCatLoggingHttpFilter</filter-class> </filter> <filter-mapping> <filter-name>LoggingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

<!-- Listener 配置 --> <listener> <listener-class>com.github.houbb.minicat.support.listener.foo.MyServletContextAttrListener</listener-class> </listener>

详细介绍

这个文件里头写的都是些XML标签,每个标签都有它自己的作用,咱们一个一个来看:

1.

:这是整个web.xml文件的根标签,所有的配置都在这个标签里面。

2.

3.

:这是Servlet的名称,用来在其他地方引用这个Servlet。

4.

:这是Servlet的完整类名,服务器会用这个类名来创建Servlet的实例。

5.

:这个标签用来定义Servlet和URL之间的映射关系。当有请求到达服务器,服务器会根据这个映射关系,决定由哪个Servlet来处理请求。

6.

(在里):它引用上面定义的Servlet的名称。

7.

:这是URL的模式,服务器会用这个模式来匹配请求的URL。在这个例子里,所有以/my结尾的请求都会由名为my的Servlet来处理。

8.

:这个标签用来定义一个Filter,Filter是Java Web应用中的一个组件,用来拦截请求和响应,做一些处理,比如日志记录、安全控制等。

9.

:这是Filter的名称,用来在其他地方引用这个Filter。

10.

:这是Filter的完整类名,服务器会用这个类名来创建Filter的实例。

11.

:这个标签用来定义Filter的过滤规则,决定哪些请求会被这个Filter处理。

12.

(在里):这是URL的模式,在这个例子里,/*表示所有的请求都会被LoggingFilter这个Filter处理。

13.

:这个标签用来定义一个Listener,Listener是Java Web应用中的一个组件,用来监听应用中的某些事件,比如应用启动、关闭等。

14.

:这是Listener的完整类名,服务器会用这个类名来创建Listener的实例。

通过这个web.xml文件,服务器就知道你的应用有哪些Servlet、Filter和Listener,以及它们都是怎么工作的。

这样,当有请求到达服务器时,服务器就能够正确地处理这些请求,把它们交给合适的Servlet来处理,让Filter在处理前后做一些额外的工作,以及在应用的生命周期中触发Listener的事件。

简单来说,web.xml就是一个告诉服务器你的Web应用是怎么运作的配置说明书。

从零手写例子

mini-cat 是简易版本的 tomcat 实现。别称嗅虎(心有猛虎,轻嗅蔷薇。)

开源地址:https://github.com/houbb/minicat

References

[1]从零手写实现 apache Tomcat-01-入门介绍:https://houbb.github.io/2016/11/07/web-server-tomcat-02-hand-write-overview

[2]从零手写实现 apache Tomcat-02-web.xml 入门详细介绍:https://houbb.github.io/2016/11/07/web-server-tomcat-02-hand-write-web-xml

[3]从零手写实现 tomcat-03-基本的 socket 实现:https://houbb.github.io/2016/11/07/web-server-tomcat-03-hand-write-simple-socket

[4]从零手写实现 tomcat-04-请求和响应的抽象:https://houbb.github.io/2016/11/07/web-server-tomcat-04-hand-write-request-and-resp

[5]从零手写实现 tomcat-05-servlet 处理支持:https://houbb.github.io/2016/11/07/web-server-tomcat-05-hand-write-servlet-web-xml

[6]从零手写实现 tomcat-06-servlet bio/thread/nio/netty 池化处理:https://houbb.github.io/2016/11/07/web-server-tomcat-06-hand-write-thread-pool

[7]从零手写实现 tomcat-07-war 如何解析处理三方的 war 包?:https://houbb.github.io/2016/11/07/web-server-tomcat-07-hand-write-war

[8]从零手写实现 tomcat-08-tomcat 如何与 springboot 集成?:https://houbb.github.io/2016/11/07/web-server-tomcat-08-hand-write-embed

[9]从零手写实现 tomcat-09-servlet 处理类:https://houbb.github.io/2016/11/07/web-server-tomcat-09-hand-write-servlet

[10]从零手写实现 tomcat-10-static resource 静态资源文件:https://houbb.github.io/2016/11/07/web-server-tomcat-10-hand-write-static-resource

[11]从零手写实现 tomcat-11-filter 过滤器:https://houbb.github.io/2016/11/07/web-server-tomcat-11-hand-write-filter

[12]从零手写实现 tomcat-12-listener 监听器:https://houbb.github.io/2016/11/07/web-server-tomcat-12-hand-write-listener

  • 发表于:
  • 原文链接https://page.om.qq.com/page/OJao4o47ZCwVWTVhzuYPsDog0
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券