Helicopters and multiples



  • Question on this code:

    1. As far as I know, Servlet Container is responsible for the velocity of the shells.

    2. But there is an option that, despite this, there may be a problem with stream security as in this code.

    My question is: Why does this code make a variable counter count not to be insecure?

    package threadSafety;
    import java.io.IOException;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.math.*;
    public class SimpleServlet extends HttpServlet
    {
    //A variable that is NOT thread-safe!
    private int counter = 0;
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
    {
    doPost(req, resp);
    }
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
    {
    resp.getWriter().println("<HTML><BODY>");
    resp.getWriter().println(this + ": <br>");
    for (int c = 0; c < 10; c++)
    {
      resp.getWriter().println("Counter = " + counter + "<BR>");
      try
      {
        Thread.currentThread().sleep((long) Math.random() * 1000);
        counter++;
      }
      catch (InterruptedException exc) { }
    }
    resp.getWriter().println("</BODY></HTML>");
    }
    }
    


  • The creation of own fluxes in a silver container is not a good practice. If there's a way, you'd better go. https://docs.oracle.com/javaee/7/tutorial/servlets012.htm ♪

    Otherwise, you yourself have to be responsible for the velocity, the disengagement, and over control of your flows.

    Answering your questions:

    1. As far as I know, Servlet Container is responsible for the velocity of the shells.

    For the flows you've created, no.

    2. But there is an option that, despite this, there may be a problem with stream security as in this code.

    Because you have a variable level copy copy, and the container creates only one copy of his class for each silver. https://java.net/downloads/servlet-spec/Final/servlet-3_1-final.pdf - Chapter 2, paragraph 2.2.), then this variable will be common to all requests and may modify it accordingly by several requests simultaneously.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2