Servlet JSP thread Learning

Source: Internet
Author: User

When compiling Servlet JSP, the Servlet JSP thread security issue is easily overlooked. If you ignore this issue, your program may have potential risks.

1. Servlet Lifecycle

The Servlet lifecycle is the responsibility of the Web Container. When the client requests the Servlet for the first time, the container initializes the Servlet, that is, instantiates the Servlet class. In the future, this instance will be responsible for client requests. Generally, other Servlet classes will not be instantiated, that is, multiple threads are using this instance. Servlet is more efficient than CGI because Servlet is multi-threaded. If the Servlet is declared as a single-threaded model, the container maintains an instance pool and multiple instances exist.

2. Servlet thread security

The Servlet specification has declared that Servlet is NOT thread-safe, so you should note this issue when developing Servlet. Here we use a realistic model to illustrate the problem. First we define a Servlet class, and then a SmulateMultiThread class and WebContainer class.

 
  1. Importjavax. servlet. http. HttpServlet;
  2. Importjavax. servlet. ServletException;
  3. Importjavax. servlet. http. HttpServletRequest;
  4. Importjavax. servlet. http. HttpServletResponse;
  5. Importjava. io. IOException;
  6. // This class simulates the multi-thread Servlet situation
  7. PublicclassSmulateMultiThreadimplementsRunnable {publicSmulateMultiThread (){
  8. }
  9. Publicstaticvoidmain (String [] args ){
  10. // Process 100 requests (Inti=0; I<100; I ++ ){
  11. NewThread (newSmulateMultiThread (). start ();
  12. }
  13. }
  14. Publicvoidrun (){
  15. HttpServletRequestrequest=Null;
  16. HttpServletResponseresponse=Null;
  17. Try {
  18. WebContainer. getServlet (). doGet (request, response );
  19. }
  20. Catch (IOExceptionex ){
  21. }
  22. Catch (ServletExceptionex ){
  23. }
  24. }
  25. }
  26. // This is a Servlet class classUnsafeServletextendsHttpServlet {
  27. PrivateStringunsafe; publicvoidinit () throwsServletException {
  28. }
  29. // ProcesstheHTTPGetrequestpublicvoiddoGet (HttpServletRequestrequest,
    HttpServletResponseresponse) throwsServletException, IOException {
  30. Unsafe=Thread. CurrentThread (). getName ();
  31. System. out. println (unsafe );
  32. }
  33. }
  34. // This is the container class classWebContainer {
  35. PrivatestaticUnsafeServletus=NewUnsafeServlet();
  36. PublicstaticUnsafeServletgetServlet () {returnus;
  37. }
  38. }

Output 100 different thread names. If 100 requests are processed by this Servlet at the same time, unsafe may have 100 types of de-value, and the client will get an error value. For example, the thread name requested by client 1 is thread-1, but the returned value may be thread-20. In reality, the user name I log on to is user1, Which is changed to user2.

So how can we ensure Servlet security? If multiple threads can be shared, do not use it (instance variables + class variables. You can also use the synchronized synchronization method, but the efficiency is not high. You can also use a single-threaded model, which is less efficient. When 100 requests come at the same time, you need to instantiate 100 instances.

The temporary variables in the method do not affect thread security because they allocate space on the stack and each thread has its own private stack space.

3. Servlet JSP thread security

JSP is essentially a Servlet. All JSP security problems should be easily understood as long as you understand the Servlet security issues. The declared variables are Servlet instance variables, not thread-safe. Others are thread-safe.

Summary: Servlet JSP threads are mainly caused by instance variables. Do not use instance variables in Servlet, JSP, or Struts actions. Do not use instance variables in any method, your program is thread-safe.

  1. JSP development technical application details
  2. Detailed description of JSP development environment Configuration
  3. JScript, Java, JavaScript, and JSP Summary
  4. Detailed description of JSP development environment Configuration
  5. ASP and JSP comparison

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.