how Apache Tomcat uses Servlets to quickly deliver a wide range of dynamic content to a client(翻译)

How Tomcat Works With Servlets

Servlet只需要处理一小部分data transaction。举个例子就是我们永远不会写servlet代码来监听来自某个端口的请求,也不会直接和客户端communicate,也不会负责管理访问控制。这些都有Tomcat(作为servlet的容器)管理。

这样可以保证servlets可以被广泛地在各种环境复用,或为了组件(components)可以同步地被其他人开发-这样就只需要改servelet代码自身而不需要担心引起别的改变。

Servlet Life Cycles

Servlet起始于容器载入servlet类(通常在响应请求的时候),结束于容器调用“destroy”方法关闭servlet。在Servlet生命周期内通常遵循如下机制:

  1. Tomcat通过它的connector收到来自客户端的请求
  2. Tomcat把请求匹配给合适的Engine来处理。这些Engines被包括在其他elements中,比如Hosts和Servers,这限制了Tomcat搜索Engine的范围
  3. 一旦请求被匹配给了相应的servelet,Tomcat会检查对应的servlet类是否被加载。如果没有被加载,Tomcat会编译servlet到Java bytecode,被JVM执行,创建servlet实例。
  4. Tomcat通过init方法初始化servlet。Servlet中有代码可以读取Tomcat的配置文件并作出相应的act,并声明任何它需要的resources。Tomcat可以有序地创建这些。
  5. 一旦servlet被初始化,Tomcat可以调用servlet的service方法来处理请求,并返回响应对象。
  6. 在servlet的生命周期中,Tomcat和servlet可以通过listener类来communicate。listener类可以监控servlet的状态变化。Tomcat可以取得并储存这些状态变化,并允许其他servlets访问。一个例子就是一个电子商务应用,一个用户将某物添加到购物车,这一变化可以同时被传递到checkout process。
  7. Tomcat调用destroy方法来慢慢移除servlet,这一action可以被很多种情况触发,比如状态改变被监听到,或者外部指令被传递到tomcat,关闭server等。

原文:

How Tomcat Works With Servlets

One of the key requirements worked into the Servlet specification is that they only are expected to handle certain parts of the total data transaction process. For example, the servlet code itself will never listen for requests on a certain port, nor will it communicate directly with a client, nor is it responsible for managing its access to resources. Rather, these things are managed by Tomcat, the servlet container.

This allows servlets to be re-used in a wide variety of environments, or for components to be developed asynchronously from one another - a connector can be re-factored for improved efficiency without any changes to the servlet code itself, as long as no major changes are made.

Servlet Life Cycles

As managed components, servlets have a life cycle, which begins when the managing container loads the servlet class, usually in response to a request, and ends when the container closes the servlet by calling the “destroy” method. All the servlet’s activity between these two points is considered part of its life cycle.

The lifecycle of a typical servlet running on Tomcat might look something like this:

Tomcat receives a request from a client through one of its connectors.
Tomcat maps this request to the appropriate Engine for processing. These Engines are contained within other elements, such as Hosts and Servers, which limit the scope of Tomcat’s search for the correct Engine.
Once the request has been mapped to the appropriate servlet, Tomcat checks to see if that servlet class has been loaded. If it has not, Tomcat compiles the servlet into Java bytecode, which is executable by the JVM, and creates an instance of the servlet.
Tomcat initializes the servlet by calling its init method. The servlet includes code that is able to read Tomcat configuration files and act accordingly, as well as declare any resources it might need, so that Tomcat can create them in an orderly, managed fashion.
Once the servlet has been initialized, Tomcat can call the servlet’s service method to process the request, which will be returned as a response.
During the servlet’s lifecycle, Tomcat and the servlet can communicate through the use of listener classes, which monitor the servlet for a variety of state changes. Tomcat can retrieve and store these state changes in a variety of ways, and allow other servlets access to them, allowing state to be maintained and accessed by various components of a given context across the span of a single or multiple user sessions. An example of this functionality in action is an e-commerce application that remembers what the user has added to their cart and is able to pass this data to a checkout process.
Tomcat calls the servlet’s destroy method to smoothly remove the servlet. This action is triggered either by a state change that is being listened for, or by an external command delivered to Tomcat to undeploy the servlet’s Context or shut down the server.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值