Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

few intrvw questions

 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
1)I have synchronized doGet() and doPost() methods in my servlets. Will this create any problem? Should i use the single thread model instead?
2)Why shouldn't we make the service() method synchronized?

3) How can we find out the number of ServletContext currently active in a servlet container?

4) I have a global variable in a servlet class. What will happen to this global variable if two requests hit on the same time?
Rgrds
 
Vijay Sargunam
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Manish,
Following are some possible answers:
Q1. I have synchronized doGet() and doPost() methods in my servlets. Will this create any problem? Should i use the single thread model instead?
Ans: Synchronizing the above methods will no cause any run time or compile time exceptions. However, since the servlet works
on the singleton pattern, their will be only one servlet and each time there is a thread accessing(ie. a client request) it,
other threads(ie. Other Client requests) will be waiting for the thread to exit. In our practice environment, this is not an issue, but in a web application, this will severely affect performance(ie. response time)
Making servlet a single thread is also not recommended, infact, it is deprecated from servlet 2.4 api, and maybe removed in future version. Better than both the solution is to make your servlet class thread safe.
Q2)Why shouldn't we make the service() method synchronized?
Ans: Same reason as above.
Q3) How can we find out the number of ServletContext currently active in a servlet container?
Ans: I dont think that can be found out directly. However, you can get the servlet context object of another context.
Q4) I have a global variable in a servlet class. What will happen to this global variable if two requests hit on the same time?
Ans: Instance variable in servlet are not thread safe, since only one instance of the servlet is created and managed by the container. If you are using instance variable in your servlet class, you may find the some undesirable result, which is also not easily traceable. So, you should always use local variables, since each thread will get a different copy of the variable.
Hope this helps.
Regards,
Vijay
 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijay
Thanx a lot . Your explanations were indeed very helpful
Rgrds
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic