Hi all,
I need to keep some data through a request, across several classes and functions. The solution I took is creating a:
Code:
public static Map m = Collections.synchronizedMap(new HashMap());
in class that implements HttpServlet.
Every time I receive each request I create a new object stored in Map using Thread.currentThread().getName() as key and my object as value.
Once request has finished I remove Object from Map. I guess is thread-safe, and it works. Well, at least I didn't find any problem.
However, I think it's not polite, is it?

Now I'm thinking to modify the implementation and use a threadLocal variable to perform the same task. What do you think? Is it better?
How should I make this? I'm thinking in somethig this, and I appreciate your advices or hints:
Code:
public class MyServlet extends HttpServlet {
	public static MyThreadLocal threadLocal = new MyThreadLocal();
	[....]
	protected void doPost(...) {
		DataTO myData = new DataTO();
		threadLocal.set(data);
		[...]
	}
}
Where
Code:
public class MyThreadLocal {

	private ThreadLocal threadLocal = new ThreadLocal();
	
	public DataTO getData(){
		return (DataTO) threadLocal.get();
	}
	
	public void setData(DataTO data) {
		threadLocal.set(data);
	}
}
If this piece of code is thread-safe, then I could take object DataTO from any other part of code (outside servlet class). Furthermore, I do not need to remove object created because threadlocal should die when servlet finishes.
Is all this true?
I'm only concerned to make MyThreadLocal static, but I have no other way to keep DataTO visible through all request process.

I need some light, because I'm not able to find useful samples in internet. It even confuses me. For sample, I've seen a comment that this is not a good a idea:
"Every new client request generates a new thread" -- actually, the servlet engines are free to implement thread pools, which means there's a chance that two successive doGets will happen on the same actual thread. You shouldn't make any assumptions about this either (in case you're tempted to use ThreadLocal variables or some other bad idea
This comment is taken from: http://www.jguru.com/faq/view.jsp?EID=150

So, I'm quite confused.

Thanks a lot for any help.

Albert.