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 ...
随机推荐
- Windows Azure免费空间如何搭建PHP网站/数据库、域名绑定
7月份,阿象为大伙介绍了中国版Windows Azure如何建站.自定义远程虚拟机,最高可选四核.28G内存的服务器,相信不少站长.开发者用户大呼过瘾.不过Azure建站系统仅支持SQL数据库,并不支 ...
- HTTP协议中PUT和POST使用区别 【转载】
有的观点认为,应该用 POST来创建一个资源,用PUT来更新一个资源:有的观点认为,应该用PUT来创建一个资源,用POST来更新一个资源:还有的观点认为可以用PUT和 POST中任何一个来做创建或者更 ...
- postgresql 触发器
一.创建事件触发器 1.ddl_command_start - 一个DDL开始执行前被触发: 2.ddl_command_end - 一个DLL 执行完成后被触发: 3.sql_drop -- 删除一 ...
- (09)odoo工作流
--------------修订时间14:54 2016-09-18 星期日21:59 2016-06-12 星期日10:06 2016-02-24 星期三17:14 2016-01-29 星期五-- ...
- hdu----(2586)How far away ?(DFS/LCA/RMQ)
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu-------(1698)Just a Hook(线段树区间更新)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- java(课程设计之记事本界面部分代码公布)
代码:涉及记事本的一些界面......!! /* *java课程设计之记事本(coder @Gxjun) * 编写一个记事本程序 * 要求: * 用图形用户界面实现. * 能实现编辑.保存.另存为.查 ...
- Intellij IDEA 安装 Mybatis插件
1.Ctrl+Alt+s
- 使用Astah繪製UML圖形(转)
http://www.dotblogs.com.tw/clark/archive/2015/02/12/149483.aspx
- BZOJ3057 圣主的考验
Poetize11的T3 DP神优化= =反正蒟蒻不会 Orz ZYF竟然找到了题解,反正我是没找到T T(百度空间:你太沙茶了,不给你看题解2333) 然后就对着标程写了一遍,然后T了...233 ...