aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes another mock exam question - jwebplus 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 "another mock exam question - jwebplus" Watch "another mock exam question - jwebplus" New topic
Author

another mock exam question - jwebplus

Sushma Sharma
Ranch Hand

Joined: Jun 02, 2005
Posts: 139
Here is another mock exam question :

Consider the following servlet.

import javax.servlet.*;
import javax.servlet.http.*:
public class SafeServlet extends HttpServlet implements SingleThreadModel
{
StringBuffer safeBuffer = new StringBuffer("I am thread safe!");
public void doGet(HttpServletRequest req, HttpServletResponse res)
{
safeBuffer.append(req.getRemoteAddr());
}
}

Which of the following statements is correct?

A. Remote addresses of ALL the requests will be successfully appended to the same string buffer.
B. It'll try to append the remote addresses of ALL the requests to the same StringBuffer Object, but it may get corrupted because of simultaneous requests.
C. for each request, a new servlet instance will be created and so the same string buffer will not contain all the remote addresses.
D. all remote address will be appended successfully, but not to the same string buffer.
E. Whether the servlet is thread safe or not depends on its deployment descriptor settings.


The answer given is D. But I think C is also correct.
Can anyone explain why only D is correct, not C?

Regards,

Sushma
AmitKumar Jain
Ranch Hand

Joined: Jun 13, 2005
Posts: 95
Hi Sushma!

In case of SingleThreadModel, a new instance of servet is created only if there are no more free instances of the servlet existing in the container.
In other words, when a new request arrives, container first checks to see if a free instance exists(ie which is not currently serving any request). If no free instance exists, it creates a new instance.

(This I've not read from anywhere, but I was told by one of the interviewer during an interview)

Hence I suppose C is not correct, as its not neccessary a new instance will be created on each requested.

Now I would like to put forward my doubt. SingleThreadModel has been depreciated from servlet 2.4 specification, but still I found a mention of isThreadSafe attribute in HF book as well as other books. I don't understand how both conditions can co-exist?


SCJP 1.4 : 91%
SCWCD 1.4 : 95%
SCBCD 1.3 : 95%
SCJP 6 Upgrade : 95%
Next SCBCD 1.5
Man is not finished when he is defeated. He is finished when he quits.
sawan parihar
Ranch Hand

Joined: Aug 24, 2004
Posts: 250
SingleThreadModel is discouraged as it affects the preformance of the app drastically.

for each request, a new servlet instance will be created and so the same string buffer will not contain all the remote addresses


This is not correct because it depends if there is any servlet instance available or not. But each request will get its own instance of the servlet.

Hope that helps.


Sawan<br />SCJP,SCWCD,SCBCD<br /> <br />Every exit is an entry somewhere.
AmitKumar Jain
Ranch Hand

Joined: Jun 13, 2005
Posts: 95
""This is not correct because it depends if there is any servlet instance available or not. But each request will get its own instance of the servlet."

Ur response is not clear to me... coz on one hand u say it depends if any instance is available or not and on other hand u say that each request creates a new instance.
sawan parihar
Ranch Hand

Joined: Aug 24, 2004
Posts: 250
Ur response is not clear to me... coz on one hand u say it depends if any instance is available or not and on other hand u say that each request creates a new instance


I wrote that "But each request will get its own instance of the servlet." It may not be a new instance. The container may reuse the instances.
AmitKumar Jain
Ranch Hand

Joined: Jun 13, 2005
Posts: 95
That means that you agree with my view.. Actually you didn't mention whether you agreed or disagreed my view.. hence I was confused...

Regards,
Amit
kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
Hi,

I was just going through this post but I would like to ask one question here.

When will the destroy method called in 'single thread model'?

Thanks
Kapil


Kapil Munjal
SCJP 1.4, SCWCD 1.4
sawan parihar
Ranch Hand

Joined: Aug 24, 2004
Posts: 250
When will the destroy method called in 'single thread model'?



When a servlet container determines that a servlet should be removed from service (for example, when a container wants to reclaim memory resources, or when it is being shut down), it calls the destroy method of the Servlet interface. The servlet shouldn't be running any service method. The server tries to ensure this completion by calling the destroy method only after service request have returned or after a server-specific grace period, whichever comes first.
[ June 27, 2005: Message edited by: sawan parihar ]
PNS Subramanian
Ranch Hand

Joined: Jul 13, 2004
Posts: 150
Firstly, do we have questions related to SingleThreadModel - thought it was deprecated from Servlet 2.4 onwards - in the examination ?

Regarding the SingleThreadModel - this may/may not create additional instances - new instances get created ONLY if there a multiple concurrent requests. This being the case what is the guarantee that a new Servlet instance (and so a StringBuffer instance) would be created ?
 
 
subject: another mock exam question - jwebplus