Java servlet Programming Reading notes-Servlet lifecycle

Source: Internet
Author: User
Servlet| Notes

The lifecycle of the
servlet is generally:





1. Create an initialization servlet





2. Handle 0 or more requests from the client





3. Destroy SERVLET,GC Reclaim memory





Each server may have a different approach to how it supports the servlet, but the servlet lifecycle is a rule that every servlet engine must follow.





instance Persistence instance persistence:





when a servlet instance loads, it starts processing all requests to this servlet, in other words, a servlet only generates one instance. This approach is good for performance improvements, can effectively reduce system overhead, and can effectively implement persistence (such as database connectivity, cache data)





Import java.io.*;import javax.servlet.*;import javax.servlet.http.*;p ublic class Simplecounter extends HttpServlet { int count = 0;public void Doget (httpservletrequest req, httpservletresponse Res) throws Servletexception, IOException {res . setContentType ("Text/plain"); PrintWriter out = Res.getwriter (); Count++;out.println ("Since loading, this servlet has been accessed" +count + "times." );}} The example above is a simple example of a servlet persistence, with each request adding the local variable count and then printing the display. However, the above example has a multithreaded risk, for each request to access the servlet can be considered a thread object, they all access the same servlet instance, so there will be concurrency problems. This is especially risky when there is a read-write operation on a shared variable, such as the local variable count in the previous example.





solution is to increase the synchronized block.





PrintWriter out = Res.getwriter (); synchronized (this) {count++;out.println ("Since loading, this servlet has been, ACCE ssed "+count +" Times ");}





in fact, the registered name for each servlet on the server corresponds to an instance of the servlet, and the request name used to access the servlet determines which instance is to handle the request,





servlet Reloading








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.