My interviewer asked , is struts Threadsafe(Is struts Action class thread safe ) . In google am getting different answers.
1. "No Struts is not thread safe " 2. Struts is not only thread-safe but also thread-dependent.
can you give me correct answer with reason.
But even Struts 2 is a Java web app, and thus based on objects that are not inherently thread-safe (like HttpSession and ServletContext), so it's perfectly possible to create thread-unsafe web apps using Struts 2. Your code needs to take appropriate measures to guard against any concurrency issues when those are involved.
I generally recommend getting a good book on Java concurrency (the ones by Oaks/Wong and Doug Lea in particular); they're really indispensible for getting a good grounding in the issues and solutions Java provides.
You have to write thread safe action classes (Have only constants as instance variables).
While that approach is sufficient for achieving thread-safety, it's not a necessary. The crucial aspect is that access to shared mutable state must be guarded. There's nothing wrong with using instance variable in servlets or Struts 1 actions, as long as they are read-only, or access to them is properly synchronized.
Note that there are other potential sources of concurrency problems besides instance variables, like sessions and context attributes.
This is one of those interview questions where I end up asking the interviewer more questions than s/he asked me in order to be able to actually answer their original question, because as asked, it's a little misleading. They may just be looking for the canonical answer, which in general, makes me not want to work for them.