Jsp/servlet Thread Safety

Source: Internet
Author: User

Thread safety is important when you are developing a servlet, or it can lead to unexpected results.

The life cycle of the servlet is the responsibility of the Web container, and when the client requests the servlet for the first time, the container is responsible for initializing the servlet, that is, instantiating the servlet class. This instance will be responsible for client requests and will not instantiate the Servlet class. That is, the servlet instance is shared by multiple threads.

So how can it be servlet security? the answer is not to use instance variables or class variables . Of course you can also use the synchronized synchronization method or use a single-threaded model, but this is inefficient.

Temporary variables do not affect thread safety because they allocate space on the stack, and each thread has its own private stack space.

JSP synchronization is also the same, because the JSP will be compiled into a servlet.

<%! in JSP String Unsafevar; The variable declared by%> is actually an instance variable of the servlet, and the variable <% String Safevar%> declares is a local variable.

Jsp/servlet Thread Safety

Related Article

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.