CGI 和 Servlet 的执行方式有何不同?

发布于 2024-08-23 22:36:45 字数 147 浏览 3 评论 0原文

我的理解是,CGI 每次都会在服务器上生成一个单独的可执行进程,但 Servlet 不会这样做 - 但我不确定如何通过比较来描述 Servlet 发生的情况。既然 Servlet 存在于 JVM 内部,并且 JVM 是一个单独的进程,那么 Servlet 与它相关的存在在哪里呢?

My understanding is that a CGI spawns a separate executable process on the server each time but that a Servlet does not do that - but I'm not sure how to describe what happens with a servlet by comparison. Since the servlet exists inside the JVM and the JVM is a single process, where does the Servlet exist in relation to it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╄→承喏 2024-08-30 22:36:45

servlet 容器(JVM 进程)通常在不同的线程中处理每个请求。

使用的最大线程数、已完成请求服务的线程是否保持活动状态以便将来重新使用等,通常都是可配置的属性。

The servlet container (JVM process) typically handles each request in a different thread.

The maximum number of threads used, if threads who have finished servicing a request are kept alive to be re-used in the future, etc., are generally all configurable attributes.

凉城凉梦凉人心 2024-08-30 22:36:45

在运行时,CGI 进程由 Web 服务器作为单独的 OS shell 启动。 shell 包括操作系统环境和执行 CGI 代码的进程,该代码驻留在服务器的文件系统中。每个新的 http 请求都会在服务器上启动一个新的操作系统 shell。 CGI 程序的响应时间很长,因为 CGI 程序在自己的 OS shell 中执行,OS shell 的创建对于操作系统来说是一项重量级活动。

对于 Servlet,它作为 Web 容器中的线程运行,而不是在单独的操作系统进程中运行。 Web容器本身是一个操作系统进程,但它作为服务运行并且持续可用。当对 servlet 的请求数量增加时,不会创建该 servlet 的其他实例。每个请求均使用一个 Java 线程同时处理。

请注意,Servlet 作为 Web 容器进程中的线程执行。

At runtime, a CGI process is launched by the web server as a separate OS shell. The shell includes an OS environment and process to execute the CGI code, which resides within the server's file system. Each new http request launches a new OS shell on the server. The response time of CGI programs is high because CGI programs execute in their own OS shell, the creation of an OS shell is a heavy-weight activity for the OS.

In the case of a servlet, it runs as a thread in the web container instead of in a separate OS process. The web container itself is an OS process, but it runs as a service and is available continuously. When the number of requests for a servlet rises, no additional instances of the servlet are created. Each request is processed concurrently using one Java thread per request.

Note that a servlet executes as a thread within the web container's process.

泛泛之交 2024-08-30 22:36:45

Servlet 代码在线程中执行。该线程由 Servlet 容器生成,Servlet 容器是在 JVM 中运行的 Java 应用程序。

收到请求后,Servlet 容器启动一个执行 Servlet 代码的线程,并且该代码将传入的请求进行处理。完成处理后,该线程将进入池或简单终止,具体取决于容器的开发方式。

好处是: 对于操作系统来说,生成一个新进程比在现有进程中生成一个线程成本更高(内存、IO 和 CPU 周期)。线程还与父进程共享内存空间。

线程可以被池化。尽管创建线程的成本较低;当然需要付出性能成本;然而,拥有线程池在某种程度上解决了这个问题。

拥有线程的另一个好处是可以优雅地完成错误处理。如果线程通过抛出错误而返回,那么处理它比处理因错误而终止的进程要容易得多。

Servlet code executes in a thread. This thread is spawned by the Servlet container which is a Java application running in JVM.

Upon receiving a request, Servlet container starts a thread which executes the servlet code and this code is given the incoming request to process. Upon finishing the processing this thread goes to either a pool or simply terminates depending upon how the container is developed.

The benefit is that: Spawning a new process is more costly (memory, IO and CPU cycles wise) for OS than spawning a thread inside an existing process. A thread also shares memory space with the parent process.

Threads can be pooled. Although a thread is less costly to create; there certainly is a performance cost to be paid; however having a pool of threads solves that to some extent.

Another good point of having Threads is that the error handling can be elegantly done. If a thread returns by throwing an error it is much easier to handle it than a process terminating with error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文