Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

another mock exam question - jwebplus

 
Sushma Sharma
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
sawan parihar
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
AmitKumar Jain
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
""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
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sawan parihar
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic