I have read servlet and JSPs are not thread safe by default. What does this statement meaning?
Also, servlet has one instance or multiple. If one then how multiple requests same time for same servlet is processed?
When JSP is first compiled, a servlet is created. Does one instance of servlet exist for this JSP? When we make jsp threadsafe by using isThreadSafe="true", what happens? how concurrency and multiple requests same time are processed for same JSP?
We say, when an object's synchnozied method is invoked by one thread, other threads has to wait. Then what happens when single servlet is accessed by multiple threads(or requests) bcoz its service method needs to be executed? how single instance of servlet would serve other threads?
The value of isThreadSafe attribute determines if a servlet instance can be re-used. If the value is true, multiple requests are handled by single servlet instance in by different incoming threads.Each thread handles a request.
In case of JSP we use isThreadSafe=true to make it threadSafe. What do we do in case of servlet to make it threadsafe? do we synchronize service method?
what if we havent made jsp or servlet threadsafe, means we havent used isThreadSafe? for each request a new instance of servlet is created? if yes, then how many instances can be created at a time for servlet in a webserver?
The best practice is to avoid synchronization in servlets and JSP, explicit using synchronized or using isThreadSafe when compiling JSP.
The main reason is that typically an application server will create a single instance of a servlet, so it is very likely all requests will be served by that single instance.
Using serialization may cause significant degradation of performance because the requests will be processed one by one instead being processed in parallel. This is especially true if the time spent in the synchronized section is significant.
Usually there is no need to synchronize the access to servlet state. Consider avoiding synchronization or at least minimizing time spent in the synchronized section. Don't use isThreadSafe because it is very coarse grained and it will synchronize the whole JSP.
1) one instance is created in webserver for a servlet for one servlet mapping.
2) one thread per client request is created
3) servlet object is a state stored in memory and same location can be accessed by multiple threads parallel or at same time. there will not be context switch type thing when multiple threads are accessing this object. when one thread is accessing object, another thread wont wait but it will also process the same method.
4) wait() will apply in case of singleton servlets. to make multiple instances of singleton servlet, we have to create multiple mappings in deployment descriptor file for same class.
The way you define 2) is incorrect. In modern web application servers there is a thread pool that serves client requests. So, 2) should be something 'Client requests are served by a set of thread (thread pool)'.
I am not sure what you mean by 'wait() will apply in case of singleton servlets'. How did wait() come into picture? Also, servlets are not singletons. A singleton is an object for that a single instance is guaranteed to exist per a classloader. This is clearly no true for servlets.
By mistake, Instead of threadsafe i used singleton here .
I have worked a lot in web applications few years back but it was solely copy paste and smart work. i never thought too much about the concepts. i am trying to clear my concepts about servlet.
wait() - when one request or thread is working with threadsafe servlet, another thread would wait till first completes its work or service() method execution.
These days, i am going thru interviews. and i dont have much time to read books... so getting knowledge of those questions which are frequently asked and for which i am very confused.
In the servlet, we can design the web page using html , and we can write the java logic code in the servlet.. Java servlet its comes under the J2EE, In the servlet we can use session and networking concepts for security..