Link to home
Start Free TrialLog in
Avatar of twg
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
Avatar of Ravindra76
Ravindra76

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of twg
twg

ASKER

My servlet connects to a database and retrieves
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?

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
Avatar of twg

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)

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.
Avatar of twg

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?
Then No need.

:)
Avatar of twg

ASKER

Thanks for all your answers