zoukankan      html  css  js  c++  java
  • How many instances created in the WebContainer

    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  

      

  • 相关阅读:
    程序命名规则
    CSS样式常用命名参考
    转:数据挖掘资料收集
    javascript占位符
    网站目录,文件夹命名规范
    IIS HTTP 500 内部服务器错误完美解决 IIS 服务器无法加载应用程序 '/LM/W3SVC/1/ROOT'。错误是 '没有注册类别
    人事工资合同管理系统菜单截图
    Vs 正则表达式 查找替换 微软权威参考
    什么是DNS,A记录,子域名,CNAME别名,MX记录,TXT记录,SRV 记录,TTL值
    MT主机控制面板Plesk 使用指南
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/3256205.html
Copyright © 2011-2022 走看看