1.servlet的工作过程以及容器的作用
Servlet没有main()方法,他们受控与另外一个java应用,这个java应用称为容器(Container)。Tomcat就是这么一个容器,web服务器应用(如Apache)得到一个指向Servlet的请求时,服务器不是把这个请求交给Servlet本身,而是交给部署该servlet的容器。要由容器向servlet提供HTTP请求和响应,而且是由容器调用servlet的方法(doGet()和doPost())。容器运行多个servlet线程来处理对同一servlet的多个请求。(对每个请求分配一个线程,而不是每个客户)
容器的作用:
1)通信支持。利用容器提供的方法,web服务器能轻松的与servlet进行通信。
2)生命周期管理。容器控制着servlet的生与死。它会负责加载类,实例化,初始化,调用servlet的方法以及使servlet能够被垃圾回收。
3)多线程支持。容器会自动的接受每个servlet请求,创建一个新的java线程。针对每个请求,如果servlet已经运行完相应的HTTP方法,线程就会结束。但是我们同时也得考虑线程的安全性。
4)JSP支持。负责将JSP翻译成真正的java。
容器是如何处理用户请求的了?
1)用户点击一个链接,指向一个servlet而不是一个静态页面。
2)web服务器接到这个请求后转发给容器。容器接着创建两个对象:HttpServletRequest和HttpServletResponse。
3)容器根据请求中的URL找到相应的servlet,为这个请求创建一个线程,并把请求对象HtttpServletRequest和响应对象HttpServletResponse传递给这个servlet线程。
4)线程接下来调用service()方法,根据请求的不同,service()方法调用doGet()和doPost()方法。
5)doGet()方法生成动态页面,并把这个页面塞到响应对象里。
6)service()方法结束,随之线程结束,容器把响应对象装换为一个HTTP相应,发送给客户,然后删除请求和响应对象。
2.Servlet的生命周期
注意他的一生都是由容器控制的。servlet一生中只有一个实例出现,但是有多个线程出现。
加载类 Servlet .class文件
实例化 构造函数运行
初始化 容器调用 init() 方法(一生只调一次)
service方法? servlet一生主要在这里度过
销 毁? 销毁实例之前调用 destroy() 方法
可回收? 等待垃圾回收等待垃圾回收
3.Servlet一生中的三大重要时刻
1)init()方法:在servlet实例被创建后调用,可以覆盖此方法做一些初始化的工作,比方说得到一个数据库连接。
2)service()方法:当客户请求到来时,容器会创建一个新的线程,调用servlet的service()方法。servlet的一生基本都在这里度过。我们一般不要覆盖此方法。
3)doGet或者doPost()方法:service方法根据请求的HTTP方法,来调用doGet()或者doPost()方法。我们在开发servlet时肯定要覆盖此方法。每次运行doGet()或者doPost()方法,它都在一个单独的线程
中。
4.容器(tomcat)在启动时会做什么?
容器在启动时,会根据相应的部署文件去寻找servlet文件。
5.为什么要有init方法了?难道不能将init代码放在构造函数中么?
因为让一个普通的java类成为servlet文件时容器付出的代价也挺大,比如说内存分配。在没有调用init方法时他还是一个普普通通的java对象,只有在用户使用时才再让他称为servlet这样对资源的利
用比较合理,注意:init方法是在第一个用户调用此servlet时被触发。
一:Servlet是什么?Servlet容器是什么?Tomcat是什么?Tomcat的组成结构以及Tomcat的工作模式。
最近没事在翻《Tomcat与Java Web开发技术详解》,本文随记一篇。
题外话:前几天做了一幅漫画,发到微博上,效果还不错~

二:Servlet是什么
为了能让Web服务器与Web应用这两个不同的软件系统协作,需要一套标准接口,Servlet就是其中最主要的一个接口。
规定:
Web服务器可以访问任意一个Web应用中实现Servlet接口的类。
Web应用中用于被Web服务器动态调用的程序代码位于Servlet接口的实现类中。
SUN公司(现在被Oracle收购了……)制定了Web应用于Web服务器进行协作的一系列标准Java接口(统称为Java Servlet API)。
SUN公司还对Web服务器发布及运行Web应用的一些细节做了规约。SUN公司把这一系列标准Java接口和规约统称为Servlet规范。
Servlet是一种运行在服务器上的小插件。
Servlet容器是什么
在Servlet规范中,把能够发布和运行JavaWeb应用的Web服务器称为Servlet容器,他的最主要特称是动态执行JavaWeb应用中的Servlet实现类中的程序代码。
Tomcat是什么
Tomcat是Servlet容器,同时也是轻量级的Web服务器。这是它的两个身份!
Apache Server、Microsoft IIS、Apache Tomcat都是Web服务器。
Tomcat作为Web服务器时,主要负责实现HTTP传输等工作。
Tomcat作为Servlet容器时,主要负责解析Request,生成ServletRequest、ServletResponse,将其传给相应的Servlet(调用service( )方法),再将Servlet的相应结果返回。
Tomcat组成结构
Server,代表整个Servlet容器组件,是Tomcat的顶层元素。其中可以包含一到多个Service;
Service,包含一个Engine,以及一到多个Connector;
Connector,代表和客户端程序实际交互的组件,负责接收客户请求,以及向客户返回响应结果;
Engine,处理同一个Service中所有Connector接收到的客户请求;
Host,在Engine中可以包含多个Host,每个Host定义了一个虚拟主机,它可以包含一个到多个Web应用;
Context,一个Host中可以包含多个Context,每个Context代表了运行在虚拟主机上的单个Web应用。
这些字段都在conf/server.xml中配置,下面是一段apache tomcat 6.0.36默认的server.xml:
- <?xml version='1.0' encoding='utf-8'?>
- <!--
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- -->
- <!-- Note: A "Server" is not itself a "Container", so you may not
- define subcomponents such as "Valves" at this level.
- Documentation at /docs/config/server.html
- -->
- <Server port="8005" shutdown="SHUTDOWN">
-
-
- <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
-
- <Listener className="org.apache.catalina.core.JasperListener" />
-
- <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
-
- <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
- <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
-
- <!-- Global JNDI resources
- Documentation at /docs/jndi-resources-howto.html
- -->
- <GlobalNamingResources>
- <!-- Editable user database that can also be used by
- UserDatabaseRealm to authenticate users
- -->
- <Resource name="UserDatabase" auth="Container"
- type="org.apache.catalina.UserDatabase"
- description="User database that can be updated and saved"
- factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
- pathname="conf/tomcat-users.xml" />
- </GlobalNamingResources>
-
- <!-- A "Service" is a collection of one or more "Connectors" that share
- a single "Container" Note: A "Service" is not itself a "Container",
- so you may not define subcomponents such as "Valves" at this level.
- Documentation at /docs/config/service.html
- -->
- <Service name="Catalina">
-
-
- <!--
- <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
- maxThreads="150" minSpareThreads="4"/>
- -->
-
-
- <!-- A "Connector" represents an endpoint by which requests are received
- and responses are returned. Documentation at :
- Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
- Java AJP Connector: /docs/config/ajp.html
- APR (HTTP/AJP) Connector: /docs/apr.html
- Define a non-SSL HTTP/1.1 Connector on port 8080
- -->
- <Connector port="8080" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443" />
-
- <!--
- <Connector executor="tomcatThreadPool"
- port="8080" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443" />
- -->
- <!-- Define a SSL HTTP/1.1 Connector on port 8443
- This connector uses the JSSE configuration, when using APR, the
- connector should be using the OpenSSL style configuration
- described in the APR documentation -->
- <!--
- <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
- maxThreads="150" scheme="https" secure="true"
- clientAuth="false" sslProtocol="TLS" />
- -->
-
-
- <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
-
-
- <!-- An Engine represents the entry point (within Catalina) that processes
- every request. The Engine implementation for Tomcat stand alone
- analyzes the HTTP headers included with the request, and passes them
- on to the appropriate Host (virtual host).
- Documentation at /docs/config/engine.html -->
-
- <!-- You should set jvmRoute to support load-balancing via AJP ie :
- <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
- -->
- <Engine name="Catalina" defaultHost="localhost">
-
- <!--For clustering, please take a look at documentation at:
- /docs/cluster-howto.html (simple how to)
- /docs/config/cluster.html (reference documentation) -->
-
-
-
-
- <!-- The request dumper valve dumps useful debugging information about
- the request and response data received and sent by Tomcat.
- Documentation at: /docs/config/valve.html -->
-
-
-
-
- <!-- This Realm uses the UserDatabase configured in the global JNDI
- resources under the key "UserDatabase". Any edits
- that are performed against this UserDatabase are immediately
- available for use by the Realm. -->
- <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
- resourceName="UserDatabase"/>
-
- <!-- Define the default virtual host
- Note: XML Schema validation will not work with Xerces 2.2.
- -->
- <Host name="localhost" appBase="webapps"
- unpackWARs="true" autoDeploy="true"
- xmlValidation="false" xmlNamespaceAware="false">
-
- <!-- SingleSignOn valve, share authentication between web applications
- Documentation at: /docs/config/valve.html -->
-
-
-
-
- <!-- Access log processes all example.
- Documentation at: /docs/config/valve.html -->
- <!--
- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
- prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
- -->
-
- </Host>
- </Engine>
- </Service>
- </Server>
Tomcat工作模式
分为三种:
1、独立的Servlet容器
2、其他Web服务器进程内的Servlet容器
3、其他Web服务器进程外的Servlet容器
第一种,Tomcat作为独立的Web服务器单独运行,Servlet容器作为Web服务器的一部分,这也是默认工作模式。
这种模式下,Tomcat是一个独立运行的Java程序,运行在Java虚拟机进程中。
blog:http://blog.youkuaiyun.com/pirateleo