• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is the servlet filter thread-safe?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
I trace a Java webapp writen by somebody else, and she has a servlet filter like this:



After read the article ( http://tutorials.jenkov.com/java-servlets/servlet-concurrency.html ), I think that the servlet filter above is not thread-safe.

For example, in the scenario:
t=0, Thread A set the response member variable
t=1, Thread B set the response member variable
t=2, Thread A pass the response to chain.doFilter()
t=3, Thread B pass the response to chain.doFilter() => bomb!! now thread A and B use the same response instance

But in practical, no customers complain any problem.

My question is,
it's not thread-safe, and have probability to cause problems (rarely maybe), right?

 
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Peter,

Welcome to JavaRanch.

The Servlet request and response objects are created afresh for every new request and response and so by their nature they are thread safe.
In your code above the filter is called for every request, and since the filter is using the response object (the response object is thread safe) to fulfill it's objective. The filter is thread safe too.

 
Peter Chu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Lalit,

thank you for your reply,

I know that javax.servlet.servletRequest and javax.servlet.servletResponse are thread-safe and live in request scope, but my question is the instance variable in the filter:
<code>private HttpServletResponseWrapper response;</code>

Some article says that instance variable in servlet is not thread-safe, e.g., http://www.javaworld.com/javaworld/jw-07-2004/jw-0712-threadsafe.html?page=2
Is it also true for servlet filter?

thank you!

 
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Some article says that instance variable in servlet is not thread-safe, e.g., http://www.javaworld.com/javaworld/jw-07-2004/jw-0712-threadsafe.html?page=2
Is it also true for servlet filter?



Yes Peter. The same rule applies for servlets, filters , listeners and all .
 
Lalit Mehra
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Peter Chu wrote:hi Lalit,

Some article says that instance variable in servlet is not thread-safe



Yes, Instance variables are not thread safe ... as the same Servlet Instance might be in use by many different threads ... to resolve this we make use of synchronization but that can slow down the response so either the instance variables should be avoided, as they are not thread safe, or you should be ready to have a bit of delay in response, if the case be, if you use synchronization.

:-) cheers
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not even close. The ServletFilter is shared by EVERY request that matches its URL filter. So not only is it not thread-safe, but whatever you do to make it thread-safe can have a severe impact on throughput.

The sample code is evidently part of something larger, since an instance variable is being set, but isn't defined or referenced. And yes, that code is extremely unsafe, since there's absolutely no protection against 2 concurrent requests using the same instance variable in conflicting ways.
 
grapes are vegan food pellets. Eat this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic