The Artima Developer Community
Sponsored Link

Java Answers Forum
Query on java servlet

9 replies on 1 page. Most recent reply: Dec 15, 2011 5:12 AM by mandeep kalidas

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 9 replies on 1 page
rashmi maheshwari

Posts: 9
Nickname: grashmi13
Registered: Sep, 2011

Query on java servlet Posted: Sep 7, 2011 4:03 AM
Reply to this message Reply
Advertisement
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?


Tech Lead

Posts: 3
Nickname: tech20
Registered: May, 2011

Re: Query on java servlet Posted: Sep 18, 2011 10:33 PM
Reply to this message Reply
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.

Jaideep

rashmi maheshwari

Posts: 9
Nickname: grashmi13
Registered: Sep, 2011

Re: Query on java servlet Posted: Sep 18, 2011 11:03 PM
Reply to this message Reply
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?

Slava Imeshev

Posts: 114
Nickname: imeshev
Registered: Sep, 2004

Re: Query on java servlet Posted: Sep 19, 2011 2:15 PM
Reply to this message Reply
Rashmi,

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.

Hope this helps.

Regards,

Slava Imeshev
Cacheonix | Clustered Java Cache
http://www.cacheonix.com

rashmi maheshwari

Posts: 9
Nickname: grashmi13
Registered: Sep, 2011

Re: Query on java servlet Posted: Sep 20, 2011 10:31 PM
Reply to this message Reply
So far i got from this thread is:

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.

Slava Imeshev

Posts: 114
Nickname: imeshev
Registered: Sep, 2004

Re: Query on java servlet Posted: Sep 20, 2011 10:57 PM
Reply to this message Reply
Rashmi,

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.


What problem are you trying to solve, exactly?

Regards,

Slava Imeshev
Cacheonix | Clustered Java Cache
http://www.cacheonix.com

rashmi maheshwari

Posts: 9
Nickname: grashmi13
Registered: Sep, 2011

Re: Query on java servlet Posted: Sep 20, 2011 11:04 PM
Reply to this message Reply
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.

Slava Imeshev

Posts: 114
Nickname: imeshev
Registered: Sep, 2004

Re: Query on java servlet Posted: Sep 20, 2011 11:47 PM
Reply to this message Reply
Rashmi,

Asking questions in forums won't take you too far.

I strongly recommend this book. Everyone who works with Servlets should read it:

http://www.amazon.com/Java-Servlet-Programming-Jason-Hunter/dp/0596000405/ref=sr_1_4?ie=UTF8&qid=1316587412&sr=8-4

Slava

rashmi maheshwari

Posts: 9
Nickname: grashmi13
Registered: Sep, 2011

Re: Query on java servlet Posted: Sep 21, 2011 12:17 AM
Reply to this message Reply
Thanks for suggesting a useful resource.

I will get this book.

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.

thanks for the help.

mandeep kalidas

Posts: 1
Nickname: artmk001
Registered: Dec, 2011

Re: Query on java servlet Posted: Dec 15, 2011 5:12 AM
Reply to this message Reply
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..

Flat View: This topic has 9 replies on 1 page
Topic: "main" java.lang.NullPointerException Previous Topic   Next Topic Topic: creating web service using jdeveloper

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use