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.
  • The difference between GET and POST: 1.post has a body which put the request parameters (The request parameters is in the URL when the method is GET); 2.the size of the parameter data is also different; 3.parameters in GET is visable; 4 GET is idempotent, POST is not.
  • The HTTP 1.1 spec declares GET, HEAD, and PUT as idempotent, even though you CAN write a non-idemtotent doGET method yourself(but shouldn’t). POST is not considered idempotent by the HTTP 1.1 spec.
  • An HTTP GET is just for getting things, and it’s not supposed to change anything on the server, so a GET is idempotent, it can be executed more than once without any bad side effects; POST is not idempotent-the data submitted in the body of a POST might be destined for a transaction that can’t be reversed.
  • Servlet lifecycle: 1.The Container initializes a servlet by loading the class, invoking the servlet’s no-arg constructor, and calling the servlet’s init() method; 2.The init() method is called only once is a servlet’s life, and always before the servlet can service any client requests; 3.The init() method gives the servlet access to the ServletConfig and ServletContext objects, which the servlet needs to get information about the servlet configuration and the web app; 4.The Container ends a servlet’s running a service() method for a client request; 5.Every request to a servlet runs in a separate thread, there is only one instance of any particular servlet class; 6.Servlet will almost always extend javax.servlet.http.HttpServlet, from which it inherits an implementation of the service() method that takes an HttpServletRequest and an HttpServletResponse; 7.HttpServlet extends javax.servlet.GenericServlet-an abstract class that implements most of the basic servlet methods; 8.GevericServlet implements the Servlet interface; 9.Servlet classes are in one of two packages: javax.servlet or javax.servlet.http; 10.we can override the init() method, and you must override at lease one service method(doGet(), doPost(), etc.).
  • Most of the time, you we use the Response just to send data back to the client, we call two methods on the response: sertContentType() and getWriter(), after that we are simply doing I/O to write HTML to the stream, but we can also use the response to set other headers, send errors, and add cookies.
  • PrintWriter is used for printing data to a character stream, although we can still write character data to an OutputStread, this is the stream that’s designed to handle character data; OutputStream is used for writing anything else.
  • If you have ever used java.io, you can just remember: println() to a PrintWriter; write() to an ServletOutputStream. Make sure you remember that the method names for getting the sream or the writer both drop the first word in the returned type: ServletOutputStream-response.getOutputStream(); PrintWriter-response.getWriter().
  • The difference between a redirect and a request dispatch: redirect makes the client do the work while request dispatch makes something else on the server do the work.
  • When a servlet does a redirect, it’s like asking the client to call someone else instead, in this case the client is the browser, not the user, the browser makes the new call on the suer’s behalf, after the originally-requested servlet says, “Sorry, call this guy instead…”, then the user sees the new URL in the browser.
  • When a servlet does a request dispatch, it’s like asking a co-worker to take over working with a client, the co-worker ends up responding to the client, but the client doesn’t care as long as someone responds, the user never knows someone else took over, because the URL in the browser bar doesn’t change.

猜你喜欢

转载自kayonlife.iteye.com/blog/1979452