Java learning-What is Servlet and java learning servlet

Source: Internet
Author: User

Java learning-What is Servlet and java learning servlet

After learning the thirteen J2EE specifications, we just had a simple understanding of them. What we need to do next is to study them in depth. Among the thirteen specifications, JSP, Servlet, EJB, and JNDI are commonly used. Let's introduce Servlet today.
1. Concepts
A servlet is a class in the Java programming language. It is used to expand the server's performance. The server resides in an application that can be accessed through the "request-response" programming model. Although servlet can respond to any type of requests, it is generally only used to dynamically expand the Web server applications. Java Servlet technology defines an HTTP-specific servlet class for these applications.
2. Principles

When the client browser requests a Servlet from the server, after receiving the request, the server first searches the container for the existence of the Servlet instance that matches the request. If the Servlet container does not exist, the Servlet container loads and runs an instance object of this type of Servlet. Then, the container framework calls the init () method of this instance to initialize the instance, then the Servlet container runs the service () method of the instance. If the Servlet instance already exists, the container framework directly calls the service () method of the instance. When the service () method is running, the doXX () method corresponding to the user request is automatically dispatched to respond to the user's request. Generally, each Servlet class has only one instance in the container. When a request arrives, a thread is allocated to process the request.
3. Functions
When using an interactive Web site, all the content you see is displayed in the browser. In these scenarios, a Web server receives a request from you and may need to switch to another code (possibly on another server) to process the request and access data, and generate the result displayed in the browser. Servlet is the Keeper used in this process ). It resides on the Web server and processes new requests and output responses. It has nothing to do with the representation. In fact, it should not be related to the representation. You can use servlet to write a stream and add the content to the Web page. However, this is usually not a good solution because it tends to encourage a mixture of representation and business logic. Implementation process: 1. get the user's request, get the display parameters entered by the user, and obtain the implicit parameters generated by the browser-GET request 2. call a specific business method of the business class at the model layer, PASS Parameters to the business method, and interact with the database. Obtain the model data returned by the business method. 3. Process Control (forwarding and redirection) based on the model data returned by the model layer ).
4. Advantages and Disadvantages
(1) advantages and good Portability: servlets are compiled in Java and have complete Servlet API standards. Servlet programs compiled by enterprises can be easily transplanted to other servers. High execution efficiency: When a Servlet request arrives, the Servlet is activated. After the request is processed, the new request will generate a thread instead of a process. Easy to use: Servlet can easily process HTML form data, read and set HTTP headers, process cookies, and track sessions. (2) Disadvantages: too many web. xml configurations are not conducive to team development; servlet is dependent on containers and is not conducive to unit testing; servlet processing requests are limited
5. Summary
Here, we understand the Servlet operating mechanism and know that it is equivalent to a Controller. What makes us think of? The Controller in MVC, right, they have very similar functions, to put it bluntly, Servlet is a controller, which makes it easy to understand. Therefore, we need to associate New knowledge with old knowledge so that we can build our own knowledge network.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.