|
Fellas..fellas..let me attempt to come to the
rescue..
As a
developer of an MVC framework ( Theseus at www.brainopolis.com/theseus ), I
have at least some insight into this. First off, the controller servlet, or for
that matter ANY servlet can indeed be a singleton..but why? It limits a servlet
to just one thread at a time, OR, ir the servlet container wants..it can
implement a "pool" of servlet instances (for each servlet class) for you, in
which each request that comes in can be handled by an instance of the servlet
class from the pool it maintins of each servlet. However, if you follow a few
simple guidelines, you NEVER need to worry about this. First, for optimum
performance, using just one single instance of each servlet is the best way to
go. It allows multiple requests from any number of clients to all hit the same
one single instance of a servlet class. The reason this is optimal is only a
single instance is created..usually when the app is started or upon the first
request. It conserves memory, eliminates the need to create and garbage collect
servlet class instances. The hard part is..you can not use any "global"
variables in the class. However..this is simple enough to avoid because in the
doPost() or doGet() ( or service() ) method, you create all your variables
locally in the method. If you need to call another method, you pass to it the
variables it needs, such as the HttpServletRequest.
Raj,
your wrong about the effeciency bring brought down. A good MVC framework such as
my own, or Struts, using just one single instance is VERY capable of high loads
on a single server. As fast as cpus are, you can run literally hundreds of
millions of instructions per second. Even with Java being interpreted, the JIT
often converts byte code to native as it sees the need, the result being that a
single servlet will most likely never be the bottleneck in a web application
unless its inundated with WAY too many requests that the server itself can't
handle. In that case, you "scale"...add more servers, use load balancers,
etc.
To the
original poster, go grab my framework, or grab Struts. Join the Theseus or
Struts mailing list, and start using it. It will simplify your life. You don't
have to write tons of servlets..you use the MVC framework and write action
classes that hides all the servlets stuff for you. MVC implemented improperly
can definitely cause problems, but when implemented properly it will infact be
the opposite..allowing much faster web development with a lot less hassles. As
for syncronizing..you never need to worry about that. Because all variables are
created within each method, they belong to the context of each request. Keep in
mind, each time a request comes in, the servlet container creates a new thread
for that request and all variables created in the methods are part of the
thread's stack frame..so each request is "safe" from another thread. However,
don't let that confuse you..if you pass in a instance field of a servlet, ALL
requests have access to the same one instance field. This can be a good way to
keep a counter for how many requests to a given servlet have
occurred.
Feel
free to ask any questions.
|
- MVC Issue Sachin S. Khanna
- Re: MVC Issue Rajinder Sandhu
- Re: MVC Issue Sachin S. Khanna
- Re: MVC Issue Rajinder Sandhu
- Re: MVC Issue Kevin Duffey
- Re: MVC Issue Harendar Bozza
- Re: MVC Issue Rajinder Sandhu
- Re: MVC Issue Kevin Duffey
