Servlet规范中的 interface 和 class

摘自javax.servlet.*类的javadoc

interface javax.servlet.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.(life-cycle methods)

  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.
servlet在服务器中的生命周期:1.加载 ->2.初始化 - > 3.调用 - 4.销毁
 
abstract  javax.servlet.http.HttpServlet
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:
  1. doGet, if the servlet supports HTTP GET requests
  2. doPost, for HTTP POST requests
  3. doPut, for HTTP PUT requests
  4. doDelete, for HTTP DELETE requests
  5. init and destroy, to manage resources that are held for the life of the servlet
  6. getServletInfo, which the servlet uses to provide information about itself
  • service handles standard HTTP requests by dispatching them to the handler methods for each HTTP request type (the doXXX methods listed above).

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. 

 
interface javax.servlet.http.HttpServletRequest
The servlet container creates an HttpServletRequest object and passes it as an argument to the servlet's service methods (doGet, doPost, etc).
 
 
interface javax.servlet.http.HttpServletResponse
The servlet container creates an HttpServletResponse object and passes it as an argument to the servlet's service methods (doGet, doPost, etc).
 
javax.servlet.http.Cookie
     Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can uniquely identify a client, so cookies are commonly used for session management. 
 
 
javax.servlet.http.HttpSession
     Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user. 
     The servlet container uses this interface to create a session between an HTTP client and an HTTP server. The session persists for a specified time period, across more than one connection or page request from the user. A session usually corresponds to one user, who may visit a site many times. The server can maintain a session in many ways such as using cookies or rewriting URLs. 
posted @ 2013-03-27 09:43  windlaughing  阅读(320)  评论(0编辑  收藏  举报