This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is the servlet filter thread-safe?

 
Peter Chu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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?

 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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
  • 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!

 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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
 
Tim Holloway
Saloon Keeper
Posts: 18304
56
Android Eclipse IDE Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic