Java Servlet Summary

Source: Internet
Author: User

A servlet is an interface in which there are 5 methods: init, service, Destroy, Getservletinfo, Getservletconfig. For Servlets, the life cycle is Init,service,destroy three steps, where the service may be called multiple times.

Genericservlet implements the Servlet interface, which is a generic servlet, and is not protocol-agnostic.

HttpServlet inherited the Gernericservlet, increased doget, dopost and other methods, the need for developers to replicate, the default action is an error. Lastmodify was also detected in the service method to see that the situation returned 304 not Modified.

The servlet we speak of in our daily life is generally referred to as httpservlet. Thus the full lifecycle of the servlet is init, service, Doget/dopost, destroy. The Init method is called after the servlet is instantiated, and the service method is called after the request arrives.

The difference between servlet and CGI. A servlet instance can serve multiple requests, each with a separate thread, and thread pooling technology is used in the generic web container, so there is very little extra overhead for each request. Instead, each request for CGI requires a separate process to be started. The cost of the process is much larger than the thread, so the servlet performs better.

The servlet is not thread-safe, and it does not have a multi-threading protection mechanism. In a servlet, each request will have a dedicated thread to handle. If multiple requests come at the same time, the service methods in the same Servlet object are called simultaneously by those threads. In order to protect the multithreading security, the Servlet class can implement the Singlethreadmodel interface. This method can handle only one request at a time, so performance is poor. However, there are also web containers that create object pools, that is, one Servlet object per thread, which makes the performance slightly better. If you need better performance, you also need to manually add code synchronization blocks. If allowed, use threadlocal to define variables. Java-side questions often ask "is the servlet thread-safe?" "In fact, this description is easy for beginners to misunderstand," what? The famous servlet has a security problem? "Personally, the more specific description should be" Do you want to consider multithreading when you implement the service method in the servlet? Why? ”

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.