The origin and implementation process of Servlet

Source: Internet
Author: User

A Servlet is a small program that runs on a server.     This word is created in the context of Java applets, which is a small program that is sent as a separate file to a Web page, which is typically used to run on the client side, resulting in services such as user operations or mapping based on user interaction. Some programs are required on the server, often the ones that access the database based on user input. These are typically using a common Gateway interface ( COmmon GAteway INTERFACE,CGI) application is completed. However, Java is run on the server, which can be implemented using the Java programming language. On servers with large traffic volumes, the advantage of Javaservlet is that they execute faster than CGI programs. Individual user requests are activated as one thread in a single program, and withoutCreates a separate process, which means that the overhead of server-side processing requests is significantly reduced. 1. implementation process The first to support Servlet technology is the JavaSoft Java Web Server. Since then, some other Java-based WEB servers have started to support the standard Servlet API. The main function of a Servlet is to interactively browse and modify data to generate dynamic Web content. This process is: 1) The client sends the request to the server side; 2) the server sends the request information to the servlet; 3) The servlet generates the response content and passes it to the server.     The response content is dynamically generated, usually depending on the client's request; 4) The server returns the response to the client. The Servlet looks like a common Java program. The servlet imports a specific package that belongs to the Java Servlet API. Because it is the object bytecode that can be dynamically loaded from the network, it can be said that the servlet is the same as the applet to the Client, but because the servlet is running in server, they do not need a graphical user interface. In this sense, servlets are also known as Facelessobject. 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 the Servlet can respond to any type of request, it is usually used only to extend the application of the WEB server. Currently the latest version is 3.1. 2. Life cycle
    1. client requests the servlet;
    2. load Servlet class to memory;
    3. Instantiate and invoke the Init () method to initialize the Servlet;
    4. service () (Call Doget () or doPost () according to the request method, plus doget (), DoPut (), Dotrace (), DoDelete (), dooptions ());
    5. destroy ().
Loads and instantiates a Servlet. This operation is generally performed dynamically. However, the server typically provides an administrative option to force the loading and initialization of a specific Servlet at Server startup. Server creates an instance of a servlet the first client's request arrives at the Init () method of the Serverserver call servlet (which can be configured to be called when Server creates a servlet instance, in Web. XML <servlet > tab Configuration <load-on-startup> tags, configured with integer value, the lower the value of the Servlet the higher the start priority of a client request arrives Serverserver creates a request object, processes the client Request server Creates a response object that responds to the client requesting the server Activation Servlet's service () method, passing the request and response object as a parameter to the service () method to obtain information about the requested object, processing the request, accessing other resources, obtaining the required information service () Method uses the method of the response object to pass the response back to the server and eventually to the client. The service () method may activate other methods to process the request, such as doget () or doPost () or a new method developed by the programmer himself. For more client requests, the Server creates a new request and response object, and still activates the Servlet's service () method, passing the two objects as parameters to it. This repeats the above loop, but does not need to call the Init () method again. A generic Servlet is initialized only once ( only one object), the server calls the servlet's destroy () method when the servlet is no longer needed by the server (typically when the server is shut down).3 Working mode
The client sends a request to the server server to start and calls Servlet,servlet to generate the response content based on the client request and pass it to the server server to return the response to the client 4 comparison with JSP The difference between      jsp and Servlet is exactly what is being applied, and a lot of people don't understand it. Simply put, SUN first developed the Servlet, its function is relatively strong, the system design is also very advanced, just, it output HTML statement or use the old CGI way, is a sentence output, so, writing and modifying HTML is very inconvenient.      java Server Pages (JSP) is a technology that implements common static HTML and Dynamic HTML mixed coding, and JSP does not add any functionality that is inherently not possible with servlets. However, it is more convenient to write static HTML in the JSP without having to use the PRINTLN statement to output each line of HTML code. More importantly, with the separation of content and appearance, tasks of different natures in page authoring can easily be separated: for example,  html design by the page designer, while leaving room for the Servlet programmer to insert dynamic content.       Later SUN launched an ASP-like Mosaic jsp, the JSP TAG mosaic into the HTML statement, so that greatly simplifies and facilitates the design and modification of the Web page. The new network language, such as asp,php,jsp, is a mosaic language. This is how the JSP and Servlet differences work.       The difference between JSP and Servlet from the point of view of the network three layer structure, a network project at least three layers: data layer, business layer, presentation layer ( Presentation layer). Of course, it can be more complicated. Servlets are very powerful for writing business layers, but it is inconvenient to write presentation layers. JSP is mainly for the convenience of writing presentation layer design. Of course, you can write a business layer. Accustomed to  asp,php,cgi friends, often unconsciously presentation layer and business layer mixed together. According to SUN's own recommendation, the JSP should only store things related to the presentation layer, that is, only the portion of the output HTML page. All data calculations, data analysis, database connection processing, all belong to the business layer, should be placed in In Java BEANS. Java BEANS is called through JSP to achieve two-tier integration.       In fact, Microsoft recently launched the DNA technology, simply said, is asp+com/dcom technology. Similar to J Sp+beans, all presentation layers are completed by ASP and all business layers are completed by com/dcom. Through invocation, the integration is realized.       Why do you use these component technologies? Because pure asp/jsp language is very inefficient execution, if a large number of user clicks, the pure SCRIPT language quickly reached his upper limit, and component technology can greatly improve the function limit, speed up the execution speed.       On the other hand, the pure SCRIPT language mixes the presentation layer with the business layer, making the changes inconvenient and the code cannot be reused. If you want to change a place, often involves more than 10 pages of code, the use of component technology is only a reorganization of the pieces.       In summary, Servlet is an early imperfect product, write business layer is very good, write presentation layer is very smelly, and two layers mixed. So, introduce Jsp+bean, write presentation layer with JSP, write business layer with BEAN. SUN's own meaning is to replace the Servlet with a JSP in the future. This is the difference between JSP and Servlet in terms of technical updates.       However, this is not to say, learn the servlet useless, in fact, you should still get started from the servlet, JSP, and then on the Jsp+bean.       The emphasis is on: Learn JSP, do not use Java beans and integration, equal to not learn. Let's spend a little more effort on the Jsp+bean.       We can see that when both asp+com and Jsp+bean are using component technology, all components are compiled first, reside in memory, and execute quickly. So, everyone often blowing servlet/jsp first compiled in memory after the speed advantage of execution is gone.       Conversely, asp+com+iIs+nt tightly integrated, there should be a greater speed advantage to present. Moreover, asp+com+iis+nt development efficiency is very high, although a lot of bugs.         So, why use Jsp+bean? Because Java has a big future. After the Microsoft split, the operating system will be a pack and go, the application software developers must find a common development language for development, Java eminence time is up. If Microsoft's spin-off goes well, the application companies that are separating it will be the new leader in Java. The current Java big head SUN and IBM are both lifeless and disappointing. I hope the new company will inject new vitality. However, the new company is likely to start Java Standard War with the old SUN, both sides set standards, affecting the Java cross-platform.       Simple analysis of the differences between JSP and Servlet and the development of Java WEB development. As machines become faster and faster, Java's speed disadvantage can be overcome quickly.

The origin and implementation process of Servlets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.