Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

java - Servlet seems to handle multiple concurrent browser requests synchronously

As far as I know Java Servlets are handling multiple requests concurrently and I've searched through StackOverflow as well as Google, and confirmed what I thought. However I am quite confused right now, I wrote a simple servlets that seem to show blocking behaviour.

so I have a simple Servlet:

public class MyServlet extends HttpServlet 
{
    private static final long serialVersionUID = 2628320200587071622L;

    private static final Logger logger = Logger.getLogger(MyServlet.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
    {
        logger.info("[doGet] Test before");

        try {
            Thread.sleep(60000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        logger.info("[doGet] Test after");

        resp.setContentType("text/plain");
        resp.getWriter().write("OK");

    }
}

Then I have 2 browser windows, I opened at the same time that hit my Servlet. And the result is the first request blocking the 2nd one. The log also shows:

10:49:05,088 [http-8383-Processor14]  INFO MyServlet - [doGet] Test before
10:50:05,096 [http-8383-Processor14]  INFO MyServlet - [doGet] Test after
10:50:05,106 [http-8383-Processor22]  INFO MyServlet - [doGet] Test before
10:51:05,112 [http-8383-Processor22]  INFO MyServlet - [doGet] Test after

I feel like I am missing something ... Servlets supposed to be able to handle concurrent request, but it doesnt seem to be doing it. I also did the same as above on the service method instead of doGet and it does the same thing.

Any pointers?

Thanks

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your browser is apparently using the same HTTP connection in different windows. The servlet container uses a single thread per HTTP connection, not per HTTP request. You should run two physically different webbrowsers to test this properly. E.g. one Firefox and one Chrome.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...