after learning the 13 specifications of the Java EE, we only have a simple understanding of them, and what we need to do next is to study and learn in depth. In 13 specifications, more commonly used are jsp,servlet,ejb,jndi, so today we will introduce the Servlet.
1. Concept
a servlet is a class in the Java programming language that is used to extend the performance of the server, which resides in applications that can be accessed through the request-response programming model. Although a servlet can respond to any type of request, it is typically used only to dynamically extend the application of a Web server. Java servlet Technology defines an HTTP-specific servlet class for these applications.
2. Principle
When
the client browser requests a servlet from the server, the server receives the request and first retrieves to the container whether the servlet instance that matches the request already exists. If it does not exist, the servlet container is responsible for loading and instantiating an instance object of the class servlet, and the container framework is responsible for invoking the instance's Init () method to do some initialization work on the instance, and then the servlet container runs the service () method of the instance. If the Servlet instance already exists, the container framework invokes the service () method of the instance directly. At run time, the service () method automatically dispatches the Doxx () method that corresponds to the user request to respond to a user-initiated request. Typically, there is only one instance of each Servlet class in the container, and whenever a request arrives, a thread is assigned to process the request.
3. function
When
you use an interactive Web site, everything you see is displayed in the browser. Behind these scenes, there is a WEB server that receives a request from you in a session, may want to switch to other code (possibly on another server) to process the request and access the data, and generate the results displayed in the browser. A servlet is a gatekeeper (gatekeeper) for this process. It resides on the WEB server, processing the response of the new request and output. It has nothing to do with the expression, and it should not actually be related to the expression. You can use a servlet to write a stream to add content to a Web page, but that is often not a good idea because it has a tendency to encourage a mixture of business logic. Implementation process: 1. Obtains the user's request, obtains the user input the display parameter, obtains the browser generated implicit parameter--obtains the request 2. Call the specific business method of the model-level business class, pass parameters to the business method, and interact with the database. Gets the model data returned by the business method. 3. Process Control (forwarding, redirection) based on model data returned by the model layer.
4. Advantages and Disadvantages
(1) Advantages portability Good: servlet is written in the Java language, with the perfect servlet API standard, Enterprise-written servlet program, can be easily ported to other servers. High execution efficiency: when the servlet request arrives, the servlet is activated, the request is processed, the new request is awaited, and the new request generates a thread instead of a process. Easy to use: Servlets can easily process HTML form data and read and set HTTP headers, process cookies, and track sessions. (2) Cons: Web. XML configuration too much, not conducive to team development; servlet has container dependencies and is not conducive to unit testing; servlet processing requests are limited
5. Summary
here, we recognize the operation mechanism of the servlet, know that it is equivalent to a controller, which reminds us of what, that is in the MVC controller, yes, they are very similar function, plainly speaking, the servlet is a controller, so, It's easy for us to understand. So we have to link the new knowledge with the old knowledge so that we can build our own knowledge network.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
What is Java learning--servlet