Potential risks of Servlet and JSP

Source: Internet
Author: User

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 and JSP 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 {
  8. PublicSmulateMultiThread ){
  9. }
  10. PublicstaticvoidmainString [] args ){
  11. // Process 100 requests
  12. ForInti=0; I<100; I ++)
  13. {
  14. NewThreadnewSmulateMultiThread )). Start );
  15. }
  16. }
  17. Publicvoidrun ){
  18. HttpServletRequestrequest=Null;
  19. HttpServletResponseresponse=Null;
  20. Try {
  21. WebContainer. getServlet ). DoGetrequest, response );
  22. } CatchIOExceptionex ){
  23. }
  24. CatchServletExceptionex ){
  25. }
  26. }
  27. }
  28. // This is a Servlet class
  29. ClassUnsafeServletextendsHttpServlet {
  30. PrivateStringunsafe;
  31. Publicvoidinit) throwsServletException {
  32. }
  33. // ProcesstheHTTPGetrequest
  34. PublicvoiddoGetHttpServletRequestrequest, HttpServletResponseresponse)
    ThrowsServletException, IOException {
  35. Unsafe=Thread. CurrentThread ). GetName );
  36. System. out. printlnunsafe );
  37. }
  38. }
  39. // This is the container class
  40. ClassWebContainer {
  41. PrivatestaticUnsafeServletus=NewUnsafeServlet);
  42. PublicstaticUnsafeServletgetServlet ){
  43. Returnus;
  44. }
  45. }

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 this be Servlet security, if multiple threads can be shared, do not use 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. thread security in JSP

The essence of JSP is Servlet. As long as you understand the security issues of Servlet, the security issues of Servlet and JSP should be easily understood. Use <%! %> The declared variables are Servlet instance variables, not thread-safe. Others are thread-safe.

 
 
  1. <%! StringunsafeVar; %>// NOT thread-safe
  2. <% StringsafeVar; %>// Thread-safe

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

  1. Install Servlets and JSP
  2. Configure the Servlet Development Environment
  3. Future Response Servlet features
  4. Detailed explanation of JSP Server Installation
  5. Jetty-Additional Servlet container Functions

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.