twg
asked on
serving some clients concurrently
Does a servlet that serves a lot of clients at once have to implement multithreading in it (thread for each client) or the web server is the responsible for managing the clients , which means that the servlet is written simply without threads?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
THere are two options.
1. See this code
public class SurveyServlet extends HttpServlet implements SingleThreadModel {
/* typical servlet code, with no threading concerns
* in the service method. No extra code for the
* SingleThreadModel interface. */
...
}
If U declare Your servlet as like this
i.e implements SingleThreadModel,
all will be taken care by servlet engine itselt. It is thread free.
2. Use the database access code in
synchronized(this) {
}
I hope this will help
ASKER
Thanks alot Ravindra76. I have one more question that isn't perfectly clear to me-I tried to run my servlet with a few clients (on a local network) and it worked fine with and without adding the "implements SingleThreadModel".
Is this because it's only few clients on a local nt?
Does every client get a thread for itself, which calls the service method of the servlet (which means that there's only one instance of the servlet), or for each client another instance of the servlet is created?
(sorry for asking too many questions but I have to understand these points)
Is this because it's only few clients on a local nt?
Does every client get a thread for itself, which calls the service method of the servlet (which means that there's only one instance of the servlet), or for each client another instance of the servlet is created?
(sorry for asking too many questions but I have to understand these points)
1. Your local version may run succesfully with less cleints because
The time at which they submitted the request and processed request are not
matched with another client's request submission due to milli second's difference.
Try with simulate the code with out implements SingleThreadModel by
allwing more than two clients clicking submit same time ( Use 4 or 5 other windows )
You will get difference.
So use implements SingleThreadModel .
2. Through out the life cycle , only one servlet process is created and each
request to service() method will create a new Thread.
ASKER
Thanks a lot Ravindra. my last question-
If I use DBConnectionPool manager which manages the connection to the databases between the servlets and the methods in it are synchronized, Do I still have to implement the SingleThreadModel?
If I use DBConnectionPool manager which manages the connection to the databases between the servlets and the methods in it are synchronized, Do I still have to implement the SingleThreadModel?
Then No need.
:)
:)
ASKER
Thanks for all your answers
ASKER
some data according to a user's request.
a lot of web clients should be able to login to the servlet and get the data to their browser.
Do I have to add extra code to enable that or, as I read in the paragraph: "By default, servlets written by specializing the HttpServlet class can have multiple threads concurrently running its service method".Does this means without adding additional code?