日期:2014-05-16  浏览次数:20316 次

Servlets & JSP Series 4 - Being a Servlet

?Servlets & JSP Series?4 - Being a Servlet

?

  • Container’s overall role in one servlet’s life: 1.User clicks a link that has a URL to a servlet to generate a request; 2.The Container “sees” that the request is for a servlet, so the container creates two objects: a.HttpServletResponse b.HttpServletRequest; 3.The Container finds the correct servlet based on the URL in the request, creates or allocates a thread for that request, and calls the servlet’s service() method, passing the request and response object as argument; 4.The service() method figures out which servlet method to call based on the HTTP Method(GET,POST,etc.) sent by the client; 5.The servlet uses the response object to write out the response to the client, the response goes back through the container; 6.The service() method completes, so the thread either dies or returens to a container-managed thread pool, the request? and response object references fall out of scope, so these objects are toast, then The client gets the response.
  • The servlet interface says that all servlets have these five method: 1.service(ServletRequest, ServletResponse); 2.init(ServletConfig); 3.destory(); 4.getServletConfig(); 5.getServletInfo().
  • GenericServlet is an abstract class that implements most of the basic servlet methods you’ll need, including those from the Servlet interface, you will probably NEVER extend this class yourself, most of your servlet’s “servlet behavior” comes from this class: 1.service(ServletRequest, ServletResponse); 2.init(ServletConfig); 3.init(); 4.destroy(); 5.getServletConfig(); 6.getServletInfo(); 7.getInitParameter(String); 8.getInitParameterNames(); 9.getServletContext(); 10.log(String); 11.log(String, Throwable).
  • HttpServlet is also a abstract class, it implements the service() method to reflect the HTTPness of the servelt-the service() method doesn’t take just ANY old servelt request and response, but an HTTP-specific request and response: 1.service(HttpSerevletRequest, HttpServletResponse); 2.service(ServletRequest, ServletResponse); 3.doGet(); 4.doPost(); 5.doHead(); 6.doPut; 7.doTrace; 8.doDelete; 9.getModified(HttpServletRequest).
  • Most of servletness is handled by superclass methods, all we do is override the HTTP methods we need. The container runs multiple threads to process multiple requests to a single servlet.
  • Normally there aren’t multiple instances of any servlet classs, each request runs in a separate thread. Servlet is always loaded and initialized before it can sevice its first client request.
  • The constructor of the servlet makes only an object, not a servlet.
  • The HttpServletRequest methods are about HTTP things like cookies, headers, and sessions. HttpServletRequest interface adds the methods that relate to the HTTP protocol. Same thing with the response, the HttpServletResponse adds methods you care about when you are using HTTP things like errors, cookies, and headers.
  • Eight methods in HTTP 1.1: GET; POST; HEAD; TRACE; PUT; DELETE; OPTIONS; CONNECT.