What is a Servlet Container?

原文转至:https://dzone.com/articles/what-servlet-container

In this post, I write a little bit about the basic ideas of web serverServlet 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

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.

  1. The init() method is invoked during initialization stage of the servlet life cycle. It is passed an object implementing the javax.servlet.ServletConfig interface, which allows the servlet to access initialization parameters from the web application.
  2. The service() method is invoked upon each request after its initialization. Each request is serviced in its own separate thread. The web container calls the service() method of the servlet for every request. The service() method determines the kind of request being made and dispatches it to an appropriate method to handle the request.
  3. The destroy() method is invoked when the servlet object should be destroyed. It releases the resources being held.

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?

  1. Web server receives HTTP request
  2. Web server forwards the request to servlet container
  3. The servlet is dynamically retrieved and loaded into the address space of the container, if it is not in the container.
  4. The container invokes the init() method of the servlet for initialization(invoked once when the servlet is loaded first time)
  5. The container invokes the service() method of the servlet to process the HTTP request, i.e., read data in the request and formulate a response. The servlet remains in the container’s address space and can process other HTTP requests.
  6. Web server return the dynamically generated results to the correct location

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 advantage 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.

原文地址:https://www.cnblogs.com/dklig/p/11595911.html

时间: 2024-08-26 18:44:39

What is a Servlet Container?的相关文章

《how tomcat work》 搬运工 charpter2: A Simple Servlet Container

这一章介绍了servlet container,application中对应的类是主要是httpServer1,这个类是创建socket绑定端口,接受请求,然后创建request实例和response实例,根据request的uri来处理请求,如果是静态资源就创建StaticResourceProcessor实例来处理,如果是servlet请求则创建对应servlet来处理请求 这章节的request和response类继承了servletRequest接口,所以对应的pom要添加依赖项 <de

What is Servlet Container

What is Servlet Container Normal Java application’s starting point is Main(public static void main(String args[])) method. When ever java application needs to start, main method of the application should be invoked. But in case of Servlet this is not

How Tomcat Works - A Simple Servlet Container

这一章和第一章的区别就是对servlet的支持.我们看下是怎么做的. 1)首先Response和Request这两个类分别实现了ServletResponse和ServletRequest接口,这两个接口和后面用到的Servlet接口都在javax.servlet这个package下面,需要添加servlet-api这个第三方依赖到classpath才能访问,这里用的是servlet-api-2.5.所有新加的方法除了Response.getWriter() 都没有任何处理,因为暂时没有用到.

javax.servlet.Filter

javax.servlet.Filter 接口 http://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html 接口原型 public interface Filter { public void init(FilterConfig filterConfig) throws ServletException; public void doFilter(ServletRequest request, ServletResponse res

web容器与servlet容器

1. web容器好比   电视机 servlet容器好比   VCD 没有VCD你可以看电视,对吧,但是有了VCD没有电视机,你从哪看起?:) 没有servlet容器,你也可以用web容器直接访问静态页面,比如安装一个apache等,但是如果要显示jsp/servlet,你就要安装一个  servlet容器了,但是光有servlet容器是不够的,因为它要被解析成html输出,所以你仍需要一个web容器. 大多数servlet容器同时提供了web容器的功能,也就是说大多servelt可以独立运行你

08 Servlet

Servlet技术          * Servlet开发动态的Web资源的技术.                       * Servlet技术               * 在javax.servlet包下,接口.               * 一个小的java程序,运行在服务器端,接收和响应从客户端(浏览器)发送过来的请求.                        * Servlet开速入门               * 简单类,实现Servlet接口.(5个方法)   

(2.1)servlet线程安全问题

本文参考链接:http://www.yesky.com/334/1951334.shtml 摘 要:介绍了Servlet多线程机制,通过一个实例并结合Java 的内存模型说明引起Servlet线程不安全的原因,给出了保证Servlet线程安全的三种解决方案,并说明三种方案在实际开发中的取舍. Servlet/JSP技术和ASP.PHP等相比,由于其多线程运行而具有很高的执行效率.由于Servlet/JSP默认是以多线程模式执行的,所 以,在编写代码时需要非常细致地考虑多线程的安全性问题.然而,很

Servlet简介

前言 1.servlet简介 a.b/s  架构 browser/server,就是客户端采用浏览器,服务器端采用web server.浏览器和   服务器端之间采用http协议进行通讯.相对于c/s架构的优势: 1.不需要关系通讯的问题,c/s架构需要自己写代码来定义通讯协议,难度比较大. 2.浏览器不需要单独安装,可维护性更好,c/s架构需要下载客户端. 服务器端负责通讯,我们可以使用servlet/jsp技术来显示业务逻辑,处理业务逻辑. b.组件和容器 组件:符合规范的可以单独部署的程序

Socket、Servlet、Tomcat

一.Socket Socket是网络编程接口(API),使得开发者可以方便地使用TCP\IP协议进行数据的传输,其客户端与服务端的交互流程为: 二.Http协议 Web应用程序主要使用HTTP协议,但HTTP协议本身存在两个问题:无状态和内容的文本表示.对于前者来说,没有记录多个请求之间的关系,而对后者来讲,如果使用Java来编程,需要进行文本和Java类型的转换. 三.Servlet Java针对Web提出了Servlet规范,即提供了Java与HTTP协议沟通的接口,这意味着Java可以以直