📜  小服务程序 API

📅  最后修改于: 2022-05-13 01:55:44.992000             🧑  作者: Mango

小服务程序 API

Servlet 是在支持Java的 Web 服务器或应用程序服务器上运行的 Java 程序。它们用于处理从网络服务器获得的请求,处理请求,产生响应,然后将响应发送回网络服务器。在Java中,要创建 Web 应用程序,我们使用 Servlet。要创建Java Servlet,我们需要使用包含所有必要接口和类的 Servlet API。 Servlet API 有 2 个包,即

  • javax.servlet
  • javax.servlet.http

javax.servlet

  • 这个包提供了接口和类的数量来支持独立于协议的通用 servlet。
  • 这些接口和类描述和定义了 servlet 类和 servlet 容器提供的运行时环境之间的契约。

javax.servlet 包中可用的类:

Class Name

Description

GenericServletTo define a generic and protocol-independent servlet.
ServletContextAttributeEvent To generate notifications about changes to the attributes of the servlet context of a web application.
ServletContextEventTo generate notifications about changes to the servlet context of a web application.
ServletInputStreamThis class provides an input stream to read binary data from a client request.
ServletOutputStreamThis class provides an output stream for sending binary data to the client.
ServletRequestAttributeEventTo generate notifications about changes to the attributes of the servlet request in an application.
ServletRequestEventTo indicate lifecycle events for a ServletRequest.
ServletRequestWrapperThis class provides the implementation of the ServletRequest interface that can be subclassed by developers to adapt the request to a Servlet.
ServletResponseWrapperThis class provides the implementation of the ServletResponse interface that can be subclassed by developers to adapt the response from a Servlet.

javax.servlet 包中可用的接口:

Interface Name

Description

FilterTo perform filtering tasks on either the request to a resource, or on the response from a resource, or both.
FilterChainTo provide a view into the invocation chain of a filtered request for a resource to the developer by the servlet container.
FilterConfigTo pass information to a filter during initialization used by a servlet container.
RequestDispatcherIt defines an object to dispatch the request and response to any other resource, means it receives requests from the client and sends them to a servlet/HTML file/JSP file on the server.
ServletThis is the main interface that defines the methods in which all the servlets must implement. To implement this interface, write a generic servlet that extends javax.servlet.GenericServlet or an HTTP servlet that extends javax.servlet.http.HttpServlet.
ServletConfigIt defines an object created by a servlet container at the time of servlet instantiation and to pass information to the servlet during initialization.
ServletContextIt defines a set of methods that a servlet uses to communicate with its servlet container. The information related to the web application available in web.xml file is stored in ServletContext object created by container.
ServletContextAttributeListenerThe classes that implement this interface receive notifications of changes to the attribute list on the servlet context of a web application.
ServletContextListenerThe classes that implement this interface receive notifications about changes to the servlet context of the web application they are part of.
ServletRequestIt defines an object that is created by servlet container to pass client request information to a servlet.
ServletRequestAttributeListenerTo generate the notifications of request attribute changes while the request is within the scope of the web application in which the listener is registered.
ServletRequestListenerTo generate the notifications of requests coming in and out of scope in a web component.
ServletResponseIt defines an object created by servlet container to assist a servlet in sending a response to the client.

javax.servlet 包中的异常:

Exception Name

Description

ServletExceptionA general exception thrown by a servlet when it encounters difficulty.
UnavailableExceptionThrown by a servlet or filter to indicate that it is permanently or temporarily unavailable.

javax.servlet.http

  • 这个包提供了接口和类的数量来支持 HTTP servlet,它依赖于 HTTP 协议。
  • 这些接口和类描述和定义了在 HTTP 协议下运行的 servlet 类与 servlet 容器提供的运行时环境之间的契约。

javax.servlet.http 包中可用的类:

Class Name

Description

CookieCreates a cookie object. It is a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server used for session management.
HttpServletProvides an abstract class that defines methods to create an HTTP suitable servlet for a web application.
HttpServletRequestWrapperThis class provides implementation of the HttpServletRequest interface that can be subclassed to adapt the request to a Servlet.
HttpServletResponseWrapperThis class provides implementation of the HttpServletResponse interface that can be subclassed to adapt the response from a Servlet.
HttpSessionBindingEventThis events are either sent to an object that implements HttpSessionBindingListener when it is bound or unbound from a session, or to a HttpSessionAttributeListener that has been configured in the deployment descriptor when any attribute is bound, unbound or replaced in a session.
HttpSessionEventTo represent event notifications for changes to sessions within a web application.

javax.servlet.http 包中可用的接口:

Interface Name

Description

HttpServletRequestTo provide client HTTP request information for servlets. It extends the ServletRequest interface.
HttpServletResponseTo provide HTTP-specific functionality in sending a response to client. It extends the ServletResponse interface.
HttpSessionIt provides a way to identify a user across web application/web site pages and to store information about that user.
HttpSessionActivationListenerContainer to notify all the objects that are bound to a session that sessions will be passivated and that session will be activated.
HttpSessionAttributeListenerTo get notifications of changes to the attribute lists of sessions within this web application, this listener interface can be implemented.
HttpSessionBindingListenerIt causes an object to be notified by an HttpSessionBindingEvent object, when it is bound to or unbound from a session.
HttpSessionListenerTo receive notification events related to the changes to the list of active sessions in a web application.

GenericServlet 类

Servlet API 在 javax.servlet 包中提供了GenericServlet类。

Java
public abstract class GenericServlet
extends java.lang.Object
implements Servlet, ServletConfig, java.io.Serializable


Java
public abstract class HttpServlet
extends GenericServlet
implements java.io.Serializable


  • 实现大多数 servlet 基本方法的抽象类。
  • 实现 Servlet、ServletConfig 和 Serializable 接口。
  • 独立于协议的 servlet。

通用 Servlet API 执行流程

  • 通过提供简单版本的生命周期方法 init() 和 destroy() 使编写 servlet 更容易。
  • 要编写通用 servlet,您需要扩展javax.servlet.GenericServlet类并需要重写抽象 service() 方法。

HttpServlet 类

Servlet API 在 javax.servlet.http 包中提供了HttpServlet类。

Java

public abstract class HttpServlet
extends GenericServlet
implements java.io.Serializable
  • 要子类化以创建适用于网站/Web 应用程序的特定于 HTTP 的 servlet 的抽象类。
  • 扩展 Generic Servlet 类并实现 Serializable 接口。
  • HTTP 协议相关的 servlet。

HTTP Servlet API 执行流程

要编写 Http servlet,您需要扩展javax.servlet.http.HttpServlet类,并且必须至少覆盖以下方法之一,

  • doGet() – 支持 servlet 的 HTTP GET 请求。
  • doPost() – 支持 servlet 的 HTTP POST 请求。
  • doPut() – 支持 servlet 的 HTTP PUT 请求。
  • doDelete() – 支持 servlet 的 HTTP DELETE 请求。
  • init() 和 destroy() – 管理 servlet 生命周期中持有的资源。
  • getServletInfo() – 提供有关 servlet 本身的信息,例如 servlet 的作者或其版本等。

Servlet API 包层次结构

下面是 Generic Servlet 和 HTTP Servlet 的包层次结构。

Servlet API 包层次结构

Servlet API 提供了开发 Web 应用程序所需的所有接口、类和方法。我们需要在 Web 应用程序中添加servlet-api.jar文件以使用 servlet 功能。我们可以从 Maven Repository 下载这个 jar 文件。