Servlet brief summary

  Recently in complementing java web only, so the first phase of selection of articles summarizing aspects of the servlet.

  To put it simply, the role servlet is to provide services for customers, that is, to accept a customer's request, and then respond back. basic http servlet to complete request processing. The client is passed over a number of parameters, and then servlet to accept and use these parameters, and then respond quickly.

  Now I will servlet's lifecycle, processing customer requests threading model, inheritance system, advantages and disadvantages of the four aspects of a general introduction to the servlet.

1.servlet life cycle

// For details, see https://www.cnblogs.com/lgk8023/p/6427977.html

(1) loading the servlet class

  After the client request to the web server, Web server receives the request, and then sends the request to the servlet, the servlet container case servlet class needs loading. Needs to know the address (local address or network address) servlet class loading during servlet class, the class found by the guide addresses then loaded classes.

(2) Examples of

  After completion of loading servlet class needs to be instantiated. That is, call the constructor of the servlet, it is worth noting that this is not generated servlet objects.

(3) Initialization

  After the constructor method call is completed, will be the init servlet () function is called, this method is only called once in a lifetime servlet, servlet objects generated in this step will be.

(4) Processing Request

  In this step, calls the service servlet different () method (GenericServlet class and HttpServlet class, GenericServlet class either get or post use the service (), HttpServlet using the doGet () method when using the get method, when using the post method use doPost () method), rvlet example get information and request information by the client ServletRequest object, after the request is processed, the method call setup response ServletResponse object information.

(5) termination of service

  In this step, we call the destroy () method of termination of service.

2. processing customer requests threading model

// For details, see https://www.cnblogs.com/GtShare/p/8033637.html

  Servlet using multithreading to process the plurality of access requests simultaneously. servlet depends on a thread pool to service requests. Thread pool worker thread is actually a series of collections. Servlet uses a dispatcher thread to manage the worker thread.

  When the Servlet container receives a request, the scheduler selects a thread from the thread pool worker thread, the request is passed to the worker thread then performed by the service method of the Servlet thread. When the thread is being executed, the container received another request, scheduling the same thread from the thread pool thread to elect a new service request another worker,
the container does not care whether this request access to the same Servlet. When at the same time receive a container, then the Servlet's service () method when the concurrent execution of multiple same Servlet requests in multiple threads. Servlet container default single instance of multi-threading way to handle the request, thus reducing the overhead generated Servlet example, to enhance the response time of the request, by the number of possible for Tomcat <Connector> element threads in the pool of threads disposed in server.xml .

 3. inheritance system

  Any class inherits an interface servlet is a servlet program, there are two inherited inherited in actual use, namely GenericServlet with HttpServlet, both of which are achieved Servlet interface, but GenericServlet different classes and class HttpServlet, regardless of GenericServlet or post is get use service (), HttpServlet using the doGet () method when using the get method, doPost () method when using the post method.

4. advantages and disadvantages

  Advantages: servlet over traditional CGI (Common Gateway Interface) programming, more efficient, easier to use, more powerful, more portable, more saving investment.

  Disadvantages: too much amount web.xml configuration (not need to configure Servlet3.0 web.xml); the servlet container having a dependency is not conducive to the test unit; requesting very limited; poor display page content.

<-! I am a white, what's wrong with big God please enlighten ->

  

 

Guess you like

Origin www.cnblogs.com/duowenjia/p/10531261.html