How many instances created in the WebContainer
When the Servlet container starts, it:
- reads
web.xml
; - finds the declared Servlets in the classpath; and
- loads and instantiates each Servlet only once.
Roughly, like this:
String urlPattern = parseWebXmlAndRetrieveServletUrlPattern();
String servletClass = parseWebXmlAndRetrieveServletClass();
HttpServlet servlet = (HttpServlet) Class.forName(servletClass).newInstance();
servlet.init();
servlets.put(urlPattern, servlet); // Similar to a map interface.
Those Servlets are stored in memory and reused every time the request URL matches the Servlet's associated url-pattern
. The servlet container then executes code similar to:
for (Entry<String, HttpServlet> entry : servlets.entrySet()) {
String urlPattern = entry.getKey();
HttpServlet servlet = entry.getValue();
if (request.getRequestURL().matches(urlPattern)) {
servlet.service(request, response);
break;
}
}
The GenericServlet#service()
on its turn decides which of the doGet()
, doPost()
, etc.. to invoke based on HttpServletRequest#getMethod()
.
You see, the servletcontainer reuses the same servlet instance for every request. In other words: the servlets are shared among every request. That's why it's extremely important to write servlet code the threadsafe manner --which is actually simple: just do not assign request or session scoped data as servlet instance variables, but just as method local variables. E.g.
public class MyServlet extends HttpServlet { private Object thisIsNOTThreadSafe; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Object thisIsThreadSafe; thisIsNOTThreadSafe = request.getParameter("foo"); // BAD!! Shared among all requests!
thisIsThreadSafe = request.getParameter("foo"); // OK, this is thread safe.
}
}
(Comments: I will just add that if the same servlet class is mapped to two different urls in web.xml, then two instances are created. But the general principle still holds, one instance serves multiple requests.)
There is only one instance of the servlet which is reused for multiple requests from multiple clients. This leads to two important rules:
- don't use instance variables in a servlet, except for application-wide values, most often obtained from context parameters.
- don't make methods
synchronized
in a servlet
(same goes for servlet filters and jsps)
According to the Java Servlet Specification Version 3.0 (pp. 6-7), there will be one instance per declaration per JVM
How many instances created in the WebContainer的更多相关文章
- 【Cocoa】 Initializing View Instances Created in Interface Builder
Initializing View Instances Created in Interface Builder View instances that are created in Interfac ...
- Java 修饰符
Java语言提供了很多修饰符,主要分为以下两类: 访问修饰符 非访问修饰符 修饰符用来定义类.方法或者变量,通常放在语句的最前端.我们通过下面的例子来说明: public class classNam ...
- openswan-ipsec.conf配置说明
Name ipsec.conf - IPsec configuration and connections Description The optional ipsec.conf file speci ...
- JAVA修饰符
修饰符用来定义类.方法或者变量,通常放在语句的最前端.我们通过下面的例子来说明: public class className { // ... } private boolean myFlag; s ...
- CKEditor Html Helpers for ASP.NET MVC3 Razor/WebForms Views
一.原生方法: 在 razor 中 使用Fckeditor 编辑内容,需要引入js <script src="@Url.Content("~/fckeditor/fckedi ...
- openstack Icehouse发布
OpenStack 2014.1 (Icehouse) Release Notes General Upgrade Notes Windows packagers should use pbr 0.8 ...
- 读文档readarx.chm
readarx.chm <Tips and Techniques> Incremented AutoCAD Registry Number Ideally, a change of reg ...
- 函数buf_pool_init
/********************************************************************//** Creates the buffer pool. @ ...
- 译:Spring框架参考文档之IoC容器(未完成)
6. IoC容器 6.1 Spring IoC容器和bean介绍 这一章节介绍了Spring框架的控制反转(IoC)实现的原理.IoC也被称作依赖注入(DI).It is a process wher ...
随机推荐
- python 练习 25
Python 语言允许在一个循环体里面嵌入另一个循环. Python for 循环嵌套语法: for iterating_var in sequence: for iterating_var in s ...
- spring来了-06-事务控制
概述 编程式事务控制 自己手动控制事务,就叫做编程式事务控制. Jdbc代码: Conn.setAutoCommite(false); // 设置手动控制事务 Hibernate代码: Sessio ...
- hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- OID,主键生成策略,PO VO DTO,get和load区别,脏检查,快照,java对象的三种状态
主键生成策略 sequence 数据库端 native 数据库端 uuid 程序端 自动赋值 生成的是一个32位的16进制数 实体类需把ID改成String 类型 assigned 程序端 需手 ...
- 使用Node.js实现数据推送
业务场景:后端更新数据推送到客户端(Java部分使用Tomcat服务器). 后端推送数据的解决方案有很多,比如轮询.Comet.WebSocket. 1. 轮询对于后端来说开发成本最低,就是按照传统的 ...
- EasyUI DataGrid View
http://www.jeasyui.com/easyui/datagrid-detailview.js 前提一定要引入:datagrid-detailview.js主要是三个属性和普通的datgag ...
- Tengine vs openresty
简介 Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到 ...
- jQuery_pager.js分页
在做前端项目中,总是需要自己手写类似于这样的分页效果: 这就需要使用jQuery.pager.js文件,其使用方法为:在html中引入三个文件,分别为: <link rel="styl ...
- JDE910笔记2--OMW项目建立及简单使用[转]
1.打开JDE的OBJECT MANAGEMENT WORKBENCH.在工作区中选择ADD,建立项目并选择OMW PROJECT,添加相关信息,如下图所示 其中,ProjectID可以对应不同的数据 ...
- Assert断言测试
assert编写代码时,我们总是会做出一些假设,断言就是用于在代码中捕捉这些假设,可以将断言看作是异常处理的一种高级形式.断言表示为一些布尔表达式,程序员相信在程序中的某个特定点该表达式值为真.可以在 ...