When the Servlet container starts, it:

  1. reads web.xml;
  2. finds the declared Servlets in the classpath; and
  3. 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  

  

最新文章

  1. linux限制ftp账户的访问路径
  2. 【收藏】Android AutoLayout全新的适配方式, 堪称适配终结者
  3. Linux下开发常用 模拟 Http get和post请求
  4. Android--Retrofit+RxJava(二)
  5. Error: Error #2014: Feature is not available at this time. at flash.filesystem::File$/initDocumentsDir()
  6. Linux启动/停止/重启Mysql数据库的方法——转载
  7. MySQL5.5半同步模式
  8. ios开发之网络基础
  9. photoshop:多边形选项
  10. spoj PARTIT
  11. 华为S5300交换机配置基于接口的本地端口镜像
  12. gulp4个基础API
  13. DNS没有生效的几个原因
  14. mybatis入门-框架原理
  15. Java IO学习笔记二
  16. Rx 入门指引 (一)
  17. http和https区别及概念
  18. springboot整合mybatis出现的一些问题
  19. 如何在IDEA里给大数据项目导入该项目的相关源码(博主推荐)(类似eclipse里同一个workspace下单个子项目存在)(图文详解)
  20. Linux环境下运行简单java程序

热门文章

  1. hdu1157 快排
  2. Qt之加载QSS文件
  3. BluetoothGatt API
  4. Javascript的&quot;预编译&quot;思考
  5. MVC之超链接的寻址
  6. 求平均排序MATLAB code
  7. ASP.NET Web API中的依赖注入
  8. Javascript之对象的继承
  9. JAVA-数据库连接【转】
  10. linux ps命令介绍