1 在这里,我将描述一下web服务器、Servlet容器的基本概念,以及Servlet容器和jvm之间的关系。我想要证明的是Servlet容器不过就是一个java程序。
2 什么是web服务器
在了解什么是Servlet容器前,我们需要先知道什么是web服务器。
web服务器使用http协议传输数据。下面是一个简单的例子:一个用户在浏览器中输入一个url(例如,www.baidu.com),然后它将会获取到一个web页面。因此,web服务器做的事情是发送一个web页面给浏览器。http协议指定了传输的请求和响应的数据格式。
3 什么是Servlet容器
就上面的例子,client/server模式只是传输静态的文件,client告诉server我需要哪个文件,然后server就把它传给client。如果用户想要获取的页面是基于它的输入的,这个基本的静态文件传输功能是不够的。Servlet容器的基本思想是在服务器端使用java动态生成web页面。所以,本质上Servlet容器是server的一部分,是它和Servlet进行交互的。
4 什么是Servlet
Servlet是javax.servlet包定义的一个接口。它生命了一个Servlet生命周期中的三个基本的方法,init()、service()和destroy()。这三个基本方法由每个Servlet所实现,并且在特定的时间由服务器所触发。
4.1 init()方法在Servlet生命周期的初始化阶段被调用。它以javax.servlet.ServletConfig接口对象为参数,来获取web应用的初始化参数。
4.2 service ()方法被每个请求所触发,每个请求在自己的线程中被serviced,也就是说,每次service()方法被调用都是在自己的线程中。Servlet容器为每个请求调用service()方法。service()方法辨别请求的类型,然后把它分发给合适的处理方法。
4.3 destroy()方法在Servlet对象需要被销毁的时候调用。它释放Servlet对象所持有的资源。
Servlet也是运行在JVM上,Servlet容器负责Servlet的创建、执行和销毁。
5 Servlet容器和服务器是怎样处理一次请求的
第一步,web服务器接收到一个http请求;
第二步,web服务器将请求转发给Servlet容器;
第三步,如果对应的Servlet还没有被加载,那么该Servlet将会被动态加载进Servlet容器的地址空间;
第四步,Servlet容器触发Servlet的init()方法,该方法只会在加载的时候触发一次;
第五步,Servlet容器触发Servlet的service()方法来处理http请求,从请求中读取数据并且生成响应。这个Servlet还在Servlet容器的地址空间中,还可以处理其它的http请求;
第六步,web服务器将动态生成的响应返回给客户端。
6 jvm的角色
使用servlet来让jvm在独立的java线程中来处理每个请求是Servlet容器的一个关键的优势。每个servlet是一个具有特殊组件的java类来对应http请求。Servlet容器的主要功能是将请求转发给正确的Servlet处理,然后再把处理的结果交给web服务器。在大多数情况下,Servlet容器运行在一个jvm实例中,但是当Servlet容器需要多个jvm实例时,也有解决方案。
In this post, I write a little bit about the basic ideas of web server, Servlet container and its relation with JVM. I want to show that Servlet container is nothing more than a Java program.
1. What is a Web Server?
To know what is a Servlet container, we need to know what is a Web Server first.
A web server uses HTTP protocol to transfer data. In a simple situation, a user type in a URL (e.g. www.programcreek.com/static.html) in browser (a client), and get a web page to read. So what the server does is sending a web page to the client. The transformation is in HTTP protocol which specifies the format of request and response message.
2. What is a Servlet Container?
As we see here, the user/client can only request static webpage from the server. This is not good enough, if the user wants to read the web page based on his input. The basic idea of Servlet container is using Java to dynamically generate the web page on the server side. So servlet container is essentially a part of a web server that interacts with the servlets.
Servlet container is the container for Servlets.
3. What is a Servlet?
Servlet is an interface defined in javax.servlet package. It declares three essential methods for the life cycle of a servlet - init(), service(), and destroy(). They are implemented by every servlet(defined in SDK or self-defined) and are invoked at specific times by the server.
From the life cycle of a servlet object, we can see that servlet classes are loaded to container by class loader dynamically. Each request is in its own thread, and a servlet object can serve multiple threads at the same time(thread not safe). When it is no longer being used, it should be garbage collected by JVM.
Like any Java program, the servlet runs within a JVM. To handle the complexity of HTTP requests, the servlet container comes in. The servlet container is responsible for servlets' creation, execution and destruction.
4. How Servlet container and web server process a request?
The six steps are marked on the following diagram:
5. The role of JVM
Using servlets allows the JVM to handle each request within a separate Java thread, and this is one of the key advantages of Servlet container. Each servlet is a Java class with special elements responding to HTTP requests. The main function of Servlet contain is to forward requests to correct servlet for processing, and return the dynamically generated results to the correct location after the JVM has processed them. In most cases servlet container runs in a single JVM, but there are solutions when container need multiple JVMs.