Analysis on the action thread safety of STRUTS1 and STRUTS2

Source: Internet
Author: User

Brief analysis on the action thread safety problem of Struts1 and Struts2 http://blog.csdn.net/virgoboy2004/article/details/5876133

"Description of the problem" recently, the company arranged for me to interview Java freshman, the interviewer is generally working for more than 1 years of new people (Here I install old, in fact, I have only worked for 3 years), when asked about Struts1 and Struts2 action of the thread safety issues, Most of it is to be in a shuffle, can't answer why. So here I sort out my personal understanding.

"Answer to the question"

This is due to the working principle of the servlet. Let's start by briefly reviewing the life cycle of the servlet "initializing->init->service->destroy-> Offload".

As we all know here, when we define a servlet in Web. XML, we can give them a value of "Load-on-startup" if the servlet's Load-on-startup configuration item is greater than 0, then in the Context The container is instantiated when it is started, and tomcat loads and instantiates an object for each servlet (note: That is, every servlet that our user configures in Web. XML is instantiated as a Servlet object)

A, the following configuration indicates that two Servlet objects will be instantiated, even if they correspond to the same servlet class

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <Web-app version="2.5"
  3. xmlns="Http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
  6. Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">
  7. <servlet>
  8. <servlet-name>servlettest1</servlet-name>
  9. <servlet-class>web.servlet.servlettest1</servlet-class>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>servlettest1</servlet-name>
  13. <url-pattern>/servlet/servlettest1</url-pattern>
  14. </servlet-mapping>
  15. <servlet>
  16. <servlet-name>servlettest2</servlet-name>
  17. <servlet-class>web.servlet.servlettest1</servlet-class>
  18. </servlet>
  19. <servlet-mapping>
  20. <servlet-name>servlettest2</servlet-name>
  21. <url-pattern>/servlet/servlettest1</url-pattern>
  22. </servlet-mapping>
  23. </Web-app>


b, the following configuration indicates that only one servlet will be instantiated

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <Web-app version="2.5"
  3. xmlns="Http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
  6. Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">
  7. <servlet>
  8. <servlet-name>servlettest1</servlet-name>
  9. <servlet-class>web.servlet.servlettest1</servlet-class>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>servlettest1</servlet-name>
  13. <url-pattern>/servlet/servlettest1</url-pattern>
  14. </servlet-mapping>
  15. <servlet-mapping>
  16. <servlet-name>servlettest1</servlet-name>
  17. <url-pattern>/servlet/servlettest1</url-pattern>
  18. </servlet-mapping>
  19. </Web-app>

In other words, the Tomcat container implements a singleton schema for the servlet, and for a servlet class, there is always only one servlet object.

Let's explain why STRUTS1 is not thread-safe.

1, Struts1

Struts1 is a direct implementation of the Java Web servlet interface, so it inherits the Tomcat implementation of the servlet, and each action in the struts1 corresponds to a servlet class, So the action here is a singleton after being instantiated by Tomcat, so Struts1 has a multithreaded problem.

For example:

You define an int i = 0 in action;

This I is then manipulated in one of the methods inside the action.

This is done in the following code:

[Java]View Plaincopy
  1. Package web.servlet;
  2. Import java.io.IOException;
  3. Import Java.io.PrintWriter;
  4. Import javax.servlet.ServletException;
  5. Import Javax.servlet.http.HttpServlet;
  6. Import Javax.servlet.http.HttpServletRequest;
  7. Import Javax.servlet.http.HttpServletResponse;
  8. /**
  9. * @author Jack Zhang
  10. * @version vb1.0
  11. * @Email [Email protected]
  12. * @Date 2013-4-21
  13. */
  14. Public class ServletTest1 extends HttpServlet
  15. {
  16. public int i = 0;
  17. /** 
  18. * Constructor of the object.
  19. */
  20. Public ServletTest1 ()
  21. {
  22. super ();
  23. }
  24. /** 
  25. * The Doget method of the servlet. <br>
  26. *
  27. * This method was called when a form had its tag value method equals to get.
  28. *
  29. * @param request the request send by the client to the server
  30. * @param response The response send by the server to the client
  31. * @throws servletexception If an error occurred
  32. * @throws IOException If an error occurred
  33. */
  34. public void doget (HttpServletRequest request, httpservletresponse response)
  35. throws Servletexception,
  36. IOException
  37. {
  38. i++;
  39. Response.setcontenttype ("text/html");
  40. PrintWriter out = Response.getwriter ();
  41. Out
  42. . println ("<!  DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > ");
  43. Out.println ("<HTML>");
  44. Out.println ("
  45. Out.println ("<BODY>");
  46. Out.print ("i=" +i);
  47. Out.println ("</BODY>");
  48. Out.println ("</HTML>");
  49. Out.flush ();
  50. Out.close ();
  51. }
  52. }


When accessing this servlet, how many times do you visit, and what is the value of I?

So: We can't define attributes in action when we're using STRUTS1. To use only words can only be defined in the method.

That's why it's not a multi-threading problem to put the definition of a property into a method, and I'm sure you'll find the answer to the question about how the Java memory model allocates memory to the method in JMM.

2, Struts2

Above we understand the Struts1 inside the multithreading problem, that Struts2 how to solve this problem? In fact, the reason is very simple, because STRTUS2 will get the user's HTTP request, and then responsible for each request to instantiate an action object, but it is noted that the action object here and Struts1 inside the action object is not a concept at all, The action class inside the STRUTS1 is a servlet class, and the action class here is just a normal Java class. This is why the action inside the STRUTS1 is thread insecure, and the action inside the STRUTS2 is a thread-safe cause.

So we look back and see how struts2 is different from the struts1 of the servlet. Readers who have read the previous analysis must know that Struts1 's action does not have any wrapper on the servlet, it is a servlet interface within the Java WEB API that is implemented directly. That's why thread safety is a problem, but the struts2 bottom helps us encapsulate the servlet so developers don't have to touch the servlet directly. The specific approach is:

Strtus2 intercepts the servlet request and then instantiates an action object for each request, destroying the action object after the request has ended. As to Strtus2 specific how to do, I do not want to be redundant, we can see the Struts2 of the relevant introduction.

In Struts2 because there is no difference between the action and the normal Java class (that is, you do not have to implement a struts interface like the one in Struts1, interested friends can see for themselves), so we can use spring to manage the action of the Struts2. , this time we have to pay attention, because when we are in spring to define the bean, spring by default is a singleton mode. So at this point, you need to modify the spring configuration file---that is, modify scope to prototype.

Analysis on the action thread safety of STRUTS1 and STRUTS2

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.