What is Servlet and Servlet?

Source: Internet
Author: User

What is Servlet and Servlet?

When I write a Web application, I directly use the spring framework and have never touched the Servlet. So I don't know the root cause of the problem, so I 'd like to learn it!
1. What is Servlet?
I am dizzy when I see this word. What is it? I try to make it clear that, in the big aspect, it is a standard for generating dynamic content on the Web, that is, the basic technology used to develop Web applications; from an elementary point of view, it is an interface Servlet provided by Java. We also call the class implementing this interface Servlet. It is opposite to CGI, but it does not like CGI. Every time it accepts an Http request, it creates a process (think about Chrome ), it will reside in the memory after each execution of its first request, waiting for subsequent requests.
A Servlet application usually contains one or more servlets. A Servlet runs in a Servlet container and cannot run automatically. What is a Servlet container? My understanding is to implement the Servlet specification, and then expose some programming interfaces for us to implement our own logic. Therefore, each custom Servlet class must implement the Servlet interface, it can generate dynamic content. It does not provide static resources like Web servers. For more details, see the following articles. The Servlet container sends user requests to the Servlet application and returns the response of the Servlet application to the user. Popular Servlet containers include Tomcat and Jetty. These two are essentially Servlet containers rather than Web servers, but they integrate the work done by Web servers into containers, so no additional Web servers are required.
Ii. Servlet API
I am using Servlet 3.1.0 and have four Java packages:
Javax. servlet: includes the classes and interfaces required to communicate with Servlet containers, such as requests, responses, filtering, and listening;
Javax. servlet. http: includes the Servlet implementation class. Classes and interfaces required for communication between httpServlet and Servlet containers;
Javax. servlet. annotation: annotation required when Servlet, Filter, and Listener are used;
Javax. servlet. descriptor: type of programmatic access to configuration information;
The core of Servlet technology is the Servlet interface. Each custom class implements this interface directly or indirectly. This interface illustrates how the Servlet is called by the Servlet container. First, the container loads the Servlet class into the memory and calls a specific method in the Servlet class. In each application, each Servlet has only one instance (single instance ). Next, I will interview the methods in Servlet:

  1. Public class TestServlet implements Servlet {
  2. Private transient ServletConfig servletConfig;
  3. Public void init (ServletConfig config) throws ServletException {
  4. This. servletConfig = config;
  5. System. out. println ("init ");
  6. }
  7. Public ServletConfig getServletConfig (){
  8. Return servletConfig;
  9. }
  10. Public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException {
  11. System. out. println ("service ");
  12. }
  13. Public String getServletInfo (){
  14. Return this. servletConfig. getServletName ();
  15. }
  16. Public void destroy (){
  17. System. out. println ("destroy ");
  18. }
  19. }
In addition to customizing a Servlet class, we also need to find a way to determine how to access our custom Servlet? Obviously, you need to specify the url. There are two Methods: Through annotation, it is relatively simple, directly add Annotation on the class:
  1. @ WebServlet (name = "testServlet", urlPatterns = "/testServlet", loadOnStartup =-1)
  2. Public class TestServlet implements Servlet {}

Configure through web. xml:

  1. <Servlet>
  2. <Servlet-name> TestServlet </servlet-name>
  3. <Servlet-class> com. javaservlet. servlet. TestServlet </servlet-class>
  4. </Servlet>
  5. <Servlet-mapping>
  6. <Servlet-name> TestServlet </servlet-name>
  7. <Url-pattern>/TestServlet </url-pattern>
  8. </Servlet-mapping>

Note:
When configuring url-pattern, you can configure multiple URLs for a Servlet, that is, you can access the same servlet through multiple URLs;
When configuring url-pattern, you can also use wildcards for configuration, but there are only two fixed methods: "*. extension ", similar to" *. the other one starts with "/" and ends with "/*", similar to "/test /*";
Start the Servlet container. Here we use Tomcat. After multiple requests are sent using a browser, the output is as follows:

The Servlet interface has three lifecycle methods:
Init (): This method is not called when the Servlet container is started. It is called only when the first request comes. In fact, when to call this method is determined by the loadOnStartup attribute, if the value is not 0, the method will be called when the container starts, but the default value is-1. This method will not be called in subsequent requests, therefore, the Code related to program initialization in this method is worth noting that this method is always run after the servlet constructor;
Service (): The Servlet container calls this method each time it requests the Servlet. This is where the business logic is written;
Destroy (): When the Servlet is destroyed, the Servlet container will call this method. This occurs when the application is uninstalled or the Servlet container is closed. In addition, from the output above, every time the Code is modified and saved, the Servlet class will be reloaded. In this case, the Servlet class will be destroyed, the Servlet container will be closed and re-enabled, and resources can be cleaned up in this method;
There are two other non-life cycle methods in this interface: getServletInfo () is not very common, but only returns the description of Servlet; getServletConfig () returns the ServletConfig class instance passed in by init, servlet configuration information.
Because the Servlet instance is a single instance, in each application, multiple requests are for one object, which can easily cause concurrency problems. Therefore, you can regard it as read-only, or use the java concurrency mechanism.

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.