定义
servlet是service applet的缩写,即表示运行在服务端的应用。狭义的servlet是jdk中的一个接口,广义的解释实现了该接口的类。
继承体系
显而易见,这个体系比之前分析Spring的创建bean涉及的体系要简单的多。
接下来我们单独分析下这个体系涉及的接口
Servlet
package javax.servlet;
import java.io.IOException;
/**
* Defines methods that all servlets must implement.
*
* <p>
* 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.
*
* <p>
* To implement this interface, you can write a generic servlet that extends
* <code>javax.servlet.GenericServlet</code> or an HTTP servlet that extends
* <code>javax.servlet.http.HttpServlet</code>.
*
* <p>
* 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:
* <ol>
* <li>The servlet is constructed, then initialized with the <code>init</code>
* method.
* <li>Any calls from clients to the <code>service</code> method are handled.
* <li>The servlet is taken out of service, then destroyed with the
* <code>destroy</code> method, then garbage collected and finalized.
* </ol>
*
* <p>
* In addition to the life-cycle methods, this interface provides the
* <code>getServletConfig</code> method, which the servlet can use to get any
* startup information, and the <code>getServletInfo</code> method, which allows
* the servlet to return basic information about itself, such as author,
* version, and copyright.
*
* @see GenericServlet
* @see javax.servlet.http.HttpServlet
*/
public interface Servlet {
public void init(ServletConfig config) throws ServletException;
public ServletConfig getServletConfig();
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException;
public String getServletInfo();
public void destroy();
}
其实jdk的介绍说的很清楚,我们现在对它总结下
1.所有的Servlets都必须实现这个接口,一个Servlets就是一个运行在web服务器中的小应用,这个应用大概干了什么勒?处理从web客户端来的Request 然后返回一个Response。
2.这个接口定义了生命周期函数,如如何初始化这个Servlets,这个Servlets如