aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes few intrvw questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "few intrvw questions" Watch "few intrvw questions" New topic
Author

few intrvw questions

manish ahuja
Ranch Hand

Joined: Oct 23, 2003
Posts: 312
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

Joined: Jan 14, 2004
Posts: 11
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

Joined: Oct 23, 2003
Posts: 312
Hi Vijay
Thanx a lot . Your explanations were indeed very helpful
Rgrds
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: few intrvw questions