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
synchronizedin 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 ...
随机推荐
- mysql jdbc连接
public class JDBCTest { public static void main(String[] args) { String sql = "SELECT * FROM us ...
- jquery添加的html元素按钮为什么不执行类样式绑定的click事件
代码举例: 更多按钮: <input type="button" class="addMore" id="addMore${issue.id } ...
- lmdb简介——结合MVCC的B+树嵌入式数据库
lmdb简介 lmdb是openLDAP项目开发的嵌入式(作为一个库嵌入到宿主程序)存储引擎.其主要特性有: 基于文件映射IO(mmap) 基于B+树的key-value接口 基于MVCC(Multi ...
- 【转】数据库范式(1NF 2NF 3NF BCNF)详解一
以下内容转自:http://jacki6.iteye.com/blog/774866 --------------------------------------------分割线---------- ...
- BZOJ3942 [Usaco2015 Feb]Censoring
维护一个栈...如果栈顶出现了要被删除的字符串就全删掉就好了,判断的话...kmp就行了 /****************************************************** ...
- 在ASP.NET MVC中使用CKEditor和CkFinder
在你需要使用editor控件的页面头部添加: <head> ... <script type="text/javascript" src="/ckedi ...
- 谈谈CSS的布局,display、position、float
前言 前端一直是我的一个很大的缺憾,这段时间痛顶思痛,决定好好的把前台的东西加强,这不,在学习了一段时间js之后,在做一些小练习,却发现最基本的一些css知识却还不了解,所以便有了这篇博文. 块级元素 ...
- MySql 分组排序取时间最大的一条记录
SELECT A.* FROM digital_asset A, (SELECT name, max(last_updated) max_day FROM digital_asset GROUP BY ...
- [JS]getYear()和getFullYear()方法区别
getFullYear();总是返回4位完整的年份 getYear();当年份在1900-1999时,返回两位数字,如1980返回80,当不在这个范围时,返回同getFullYear(); 注:get ...
- bzoj 1821: [JSOI2010]Group 部落划分 Group
#include<cstdio> #include<iostream> #include<cmath> #include<algorithm> #def ...