e1041. preventing concurrent requests to a servlet.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 9 行

TXT
9
字号
By default, a servlet container will use a servlet instance to concurrently process multiple requests. Although, this behavior burdens the servlet developer to properly synchronize shared state, this ability to handle multiple requests allows for a more scalable architecture. However, if it is necessary to disable this ability, the servlet should implement the SingleThreadModel interface: 
    // This servlet can only handle a single request at a time.
    public class MyServlet extends HttpServlet implements SingleThreadModel {
        public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            // Handle GET request...
        }
    }

However, forcing the servlet container to route requests one at a time through a servlet does not prevent the container from creating multiple instances of the servlet. Hence, the servlet developer must still properly synchronize shared state. Effectively, the main benefit of implementing the SingleThreadModel is not having to synchronize the servlet's instance variables.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?