单个servlet如何处理来自客户端的多个请求 [英] How does a single servlet handle multiple requests from client side

查看:315
本文介绍了单个servlet如何处理来自客户端的多个请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单个servlet如何处理以用户请求形式出现的多个客户端请求?基于单例设计模式,我知道我们创建了一个servlet实例,但是单个servlet如何处理数百万个请求。对于涉及它的线程感到困惑。

How does a single servlet handle multiple client requests coming in the form of user requests ? Based on the singleton design pattern I know we get a single instance of servlet created , but how does a single servlet handle millions of requests . Confused about the threading involved in it also.

此处也可以使用任何浏览器规范或设置来发送请求或生成为请求发送的线程。

Also does any browser specifications or settings come handy out here for sending the requests across or generating the threads sent out for the requests.

对于所有框架是否相同,或者它与struts v / s spring不同?

Is it the same for all frameworks or it differs say for example struts v/s springs ?

推荐答案

Struts / Spring框架实际上是在Servlet规范之上编写的,所以无论你在它下面使用什么都使用Servlets。

Struts/Spring frameworks are actually written on top of Servlet specification so doesn't matter what you use underneath it use Servlets.

你是对的,只创建了一个Servlet实例,但该实例是在多个线程之间共享的。出于这个原因,你永远不应该在Servlet中共享可变状态。

You are right, Only single instance of Servlet is created, but that instance is shared across multiple threads. For this reason you should never have shared mutable states in your Servlets.

例如,你有以下servlet映射到 http:// localhost / myservlet

For example you have following servlet mapped to http://localhost/myservlet

class MySerlvet extends HttpServlet {

     public void doGet(HttpServletRequest req, HttpServletResponse res) {
          // Get Logic
     }    
}

Web服务器的代码中会有类似的东西(不一定相同)。

The Web Server will have something similar (Not necessarily same) in its code.

MyServlet m = new MyServlet(); // This will be created once

// for each request for http://localhost/myservlet
executorService.submit(new RequestProcessingThread(m));

这篇关于单个servlet如何处理来自客户端的多个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆