1、接口

1.1 public interface __Servlet(包含生命周期)

Defines methods that all servlets must implement.
A servlet is a small Java program that runs within a Web server. Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol.
To implement this interface, you can write a generic servlet that extends javax.servlet.GenericServlet or an HTTP servlet that extends javax.servlet.http.HttpServlet.
This interface defines methods to initialize a servlet, to service requests, and to remove a servlet from the server. These are known as life-cycle methods and are called in the following sequence:

  1. The servlet is constructed, then initialized with the init method.
  2. Any calls from clients to the service method are handled.
  3. The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.

In addition to the life-cycle methods, this interface provides the getServletConfig method, which the servlet can use to get any startup information, and the getServletInfo method, which allows the servlet to return basic information about itself, such as author, version, and copyright.
翻译:
公共接口Servlet
定义所有servlet必须实现的方法。
servlet是在Web服务器中运行的小型Java程序。servlet通常通过超文本传输协议HTTP接收和响应来自Web客户端的请求。
要实现此接口,可以编写扩展javax.servlet.GenericServlet的通用servlet或扩展javax.servlet.HTTP.HttpServlet的HTTP servlet。
这个接口定义了初始化servlet、服务请求和从服务器中删除servlet的方法。这些方法称为生命周期方法,按以下顺序调用:
1.构建servlet,然后用init方法初始化。
2.处理客户端对服务方法的任何调用。
3.servlet停止服务,然后用destroy方法销毁,然后垃圾收集并完成。
除了生命周期方法之外,此接口还提供getServletConfig方法(servlet可以使用该方法获取任何启动信息)和getServletInfo方法(允许servlet返回关于自身的基本信息,如作者、版本和版权)。

1.1.1 public abstract class GenericServlet

extends Object
implements Servlet, ServletConfig, Serializable
Defines a generic, protocol-independent servlet. To write an HTTP servlet for use on the Web, extend HttpServlet instead.
GenericServlet implements the Servlet and ServletConfig interfaces. GenericServlet may be directly extended by a servlet, although it’s more common to extend a protocol-specific subclass such as HttpServlet.
GenericServlet makes writing servlets easier. It provides simple versions of the lifecycle methods init and destroy and of the methods in the ServletConfig interface. GenericServlet also implements the log method, declared in the ServletContext interface.
To write a generic servlet, you need only override the abstract service method.
翻译:
定义一个通用的、独立于协议的servlet。要编写一个在Web上使用的HttpServlet,请改为扩展HttpServlet。
GenericServlet实现Servlet和ServletConfig接口。GenericServlet可以由servlet直接扩展,尽管扩展特定于协议的子类(如HttpServlet)更为常见。
GenericServlet使编写Servlet更容易。它提供了简单版本的生命周期方法init和destroy以及ServletConfig接口中的方法。GenericServlet还实现了在ServletContext接口中声明的log方法。
要编写通用servlet,只需重写抽象服务方法。

1.1.2 public abstract class HttpServlet

extends GenericServlet
Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site. A subclass of HttpServlet must override at least one method, usually one of these:

  • doGet, if the servlet supports HTTP GET requests
  • doPost, for HTTP POST requests
  • doPut, for HTTP PUT requests
  • doDelete, for HTTP DELETE requests
  • init and destroy, to manage resources that are held for the life of the servlet
  • getServletInfo, which the servlet uses to provide information about itself

There’s almost no reason to override the service method. service handles standard HTTP requests by dispatching them to the handler methods for each HTTP request type (the doXXX methods listed above).
Likewise, there’s almost no reason to override the doOptions and doTrace methods.
Servlets typically run on multithreaded servers, so be aware that a servlet must handle concurrent requests and be careful to synchronize access to shared resources. Shared resources include in-memory data such as instance or class variables and external objects such as files, database connections, and network connections. See the Java Tutorial on Multithreaded Programming for more information on handling multiple threads in a Java program.
翻译:
提供一个要子类化的抽象类,以创建适用于网站的HTTPservlet。HttpServlet的子类必须重写至少一个方法,通常是以下方法之一:
•doGet,如果servlet支持HTTP GET请求
•doPost,用于HTTP POST请求
•doPut,用于HTTP PUT请求
•doDelete,用于HTTP删除请求
•init和destroy,用于管理在servlet生命周期内保留的资源
•getServletInfo,servlet使用它来提供关于自身的信息
几乎没有必要重写service 方法。服务通过将标准HTTP请求分派到每个HTTP请求类型的处理程序方法(上面列出的doXXX方法)来处理它们。
同样,几乎没有必要重写doOptions和doTrace方法。
servlet通常在多线程服务器上运行,因此请注意servlet必须处理并发请求,并注意同步对共享资源的访问。共享资源包括内存中的数据(如实例或类变量)和外部对象(如文件、数据库连接和网络连接)。有关在Java程序中处理多线程的更多信息,请参阅Java多线程编程教程。

1.2 public interface ServletRequest

Defines an object to provide client request information to a servlet. The servlet container creates a ServletRequest object and passes it as an argument to the servlet’s service method.
A ServletRequest object provides data including parameter name and values, attributes, and an input stream. Interfaces that extend ServletRequest can provide additional protocol-specific data (for example, HTTP data is provided by HttpServletRequest.
翻译:
定义一个向servlet提供客户端请求信息的对象。servlet容器创建一个ServletRequest对象,并将其作为参数传递给servlet的服务方法。
ServletRequest对象提供包括参数名称和值属性__输入流的数据。扩展ServletRequest的接口可以提供额外的特定于协议的数据(例如,HTTP数据由HttpServletRequest提供)。

1.2.1 public interface HttpServletRequest

extends ServletRequest
Extends the ServletRequest interface to provide request information for HTTP servlets.
The servlet container creates an HttpServletRequest object and passes it as an argument to the servlet’s service methods (doGet, doPost, etc).
翻译:
扩展ServletRequest接口以提供HTTP servlet的请求信息。
servlet容器创建一个HttpServletRequest对象,并将其作为参数传递给servlet的服务方法(doGet、doPost等)。

1.3 public interface ServletResponse

Defines an object to assist a servlet in sending a response to the client. The servlet container creates a ServletResponse object and passes it as an argument to the servlet’s service method.
To send binary data in a MIME body response, use the ServletOutputStream returned by getOutputStream(). To send character data, use the PrintWriter object returned by getWriter(). To mix binary and text data, for example, to create a multipart response, use a ServletOutputStream and manage the character sections manually.
The charset for the MIME body response can be specified explicitly using any of the following techniques: per request, per web-app (using ServletContext.setRequestCharacterEncoding(java.lang.String), deployment descriptor), and per container (for all web applications deployed in that container, using vendor specific configuration). If multiple of the preceding techniques have been employed, the priority is the order listed. For per request, the charset for the response can be specified explicitly using the setCharacterEncoding(java.lang.String) and setContentType(java.lang.String) methods, or implicitly using the setLocale(java.util.Locale) method. Explicit specifications take precedence over implicit specifications. If no charset is explicitly specified, ISO-8859-1 will be used. The setCharacterEncoding, setContentType, or setLocale method must be called before getWriter and before committing the response for the character encoding to be used.
See the Internet RFCs such as RFC 2045 for more information on MIME. Protocols such as SMTP and HTTP define profiles of MIME, and those standards are still evolving.
翻译:
定义一个对象来__帮助servlet向客户机发送响应。servlet容器创建一个ServletResponse对象,并将其作为参数传递给servlet的服务方法。
要在MIME正文响应中发送二进制数据,请使用getOutputStream()返回的ServletOutputStream。要发送字符数据,请使用getWriter()返回的PrintWriter对象。要混合二进制和文本数据,例如,要创建多部分响应,请使用ServletOutputStream并手动管理字符部分。
可以使用以下任何技术显式指定MIME正文响应的字符集:每个请求、每个web应用程序(使用ServletContext.setRequestCharacterEncoding(java.lang.String)、部署描述符)和每个容器(对于部署在该容器中的所有web应用程序,使用特定于供应商的配置)。如果采用了上述多种技术,则优先顺序为所列顺序。对于每个请求,可以使用setCharacterEncoding(java.lang.String)和setContentType(java.lang.String)方法显式指定响应的字符集,或者隐式使用setLocale(java.util.Locale)方法。显式规范优先于隐式规范。如果没有明确指定字符集,将使用ISO-8859-1。setCharacterEncoding、setContentType或setLocale方法必须在getWriter之前以及提交要使用的字符编码的响应之前调用。
有关MIME的更多信息,请参阅Internet RFC(如RFC 2045)。SMTP和HTTP等协议定义了MIME的配置文件,这些标准仍在不断发展。

1.3.1 public interface HttpServletResponse

extends ServletResponse
Extends the ServletResponse interface to provide HTTP-specific functionality in sending a response. For example, it has methods to access HTTP headers and cookies.
The servlet container creates an HttpServletResponse object and passes it as an argument to the servlet’s service methods (doGet, doPost, etc).
翻译:
扩展ServletResponse接口,以便在发送响应时提供特定于HTTP的功能。例如,它有访问HTTP头和cookies的方法。
servlet容器创建一个HttpServletResponse对象,并将其作为参数传递给servlet的服务方法(doGet、doPost等)。

1.4 public interface ServletConfig

A servlet configuration object used by a servlet container to pass information to a servlet during initialization.
翻译:
公共接口ServletConfig
servlet容器用于在初始化期间向servlet传递信息的servlet配置对象。

2 、结构

2.1 Servlet

image.png

image.png

2.2 ServletConfig

image.png

2.3 ServletRequest

image.png

2.4 ServletResponse

image.png

3、总结

1、Tomcat是一个Servlet容器(或JSP容器),Tomcat负责转发消息给Servlet,而Servlet包含4个接口:_

接口名称 作用
Servlet Servlet中核心接口,包含了Servlet的生命周期
ServletConfig Servlet中的配置接口,可以获取Servlet的配置信息
ServletRequest Servlet中提供客户端的请求信息的接口。
ServletResponse Servlet中用来给客户端返回信息的接口

这四个接口都是相互独立的,没有继承或实现关系。