title: Could This LocalThread Overwrite Other Servlet Requests' Data?
body: <p>I have the following request context class that is used to collect certain information for future processing in Servlet filter:</p>
  
  <pre><code>public class ApplicationRequestContext {
    private static final ThreadLocal&lt;AppRequest&gt; tl = new ThreadLocal&lt;AppRequest&gt;();
  
    public static void begin(HttpServletRequest httpRequest) {
      tl.set(getAppRequest(httpRequest));
    }
  
    public static void finish() {
      tl.remove();
    }
  }
  </code></pre>
  
  <p><code>getAppRequest(httpRequest)</code> just returns an object. And in the Servlet filter...</p>
  
  <pre><code>public class ApplicationServletFilter implements javax.servlet.Filter {
    @Override
  
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
      try {
        ApplicationRequestContext.begin(httpRequest);
        /* other stuff */
        chain.doFilter(httpRequest, httpResponse);
      } catch (Exception e) { /* … */ }
      finally {
        ApplicationRequestContext.finish();
      }
    }
  }
  </code></pre>
  
  <p>I would like to know, could this setup have multiple incoming requests to  <strong>possibly</strong> overwrite each others' data (<code>getAppRequest(httpRequest)</code> object obviously is going to be unique)? Is this even thread-safe?</p>