File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Whether this architecture is flawless!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Whether this architecture is flawless!!!" Watch "Whether this architecture is flawless!!!" New topic

Whether this architecture is flawless!!!

Rishi Singh
Ranch Hand

Joined: Dec 09, 2000
Posts: 321
Dear all,
I have the following doubts:
I was thinking about the following architecture for a
web based application -
having a single servlet which would receive all requests.
The rest of the modules of the application can be classes.
The servlet would do
* the authentication,
* log the result of the same (who logged in, when and if
the attempt is unsuccessful - log the same)
* the connection pool object would be in the servlet context
its getConnection() method would return a connection to the
database. This method would be passed to the method of the
class instantiated

* check if the user has rights to access the module asked for
(say - prepare a sales order)
* if the user has the rights, then it would insantiate the
sales order class and call the appropriate method of the
class depending on the type of the request (prepare a new
sales order, modify an existing sales order, query an order
in short the servlet would have a huge switch case which
would take care of the business logic. These would return
the html to the servlet and the servlet calls some template files , does patern matching and outputs the
html content to the browser in one out.println() statement
do you think that such big a servlet would cause performance
issues - one because all requests to the servlet would be
handled by this one servlet and another it would instantiate
the business class required and process it.
My main question is - will the classes be reused once they are
instantiated in RAM (on the next hit) OR are we completely at the
mercy of the garbage collector (ie if he leaves the class object
in RAM then and then only would it be reused, else the next request
for the same class would need the instantiation again)?
Because servlets are internally threaded, would the object
instantiated by one thread be available to another thread?
if yes, then do i need to take care about the mutation of public
variables in the class.
in the servlets case, i feel that i may get some benefits..
but that again is a feeling - no arguments in support
EXPERIENCE...I would like to know where would this approach go haywire.
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Here is a quick Google search on what you are proposing.
I appologise for not having more time to cover the issue.
Kyle Brown
Ranch Hand

Joined: Aug 10, 2001
Posts: 3879
OK, a couple of points to comment on:
(1) DO NOT use a huge switch statement. The results of that would be truly horrifying. Instead, what single-servlet MVC frameworks traditionally do is to use reflection to determine which "controller" class will handle the request. You basically do a lookup on some parameter of the request into a hashmap and then instantiate an instance of a corresponding class to handle the request. The hashmap would contain something like this:
login --> LoginHandler
getOrder --> GetSalesOrderHandler
updateOrder --> UpdatedSalesOrderHandler
After getting an instance of the right class, then pass the request and response on to it for handling.
(2) In general, you do NOT reuse objects across threads. Unless you are a multithreaded programming wizard like Peter Haggar, it's much safer to just create new instances in automatic variables and let things be handled that way. DON'T WORRY ABOUT GARBAGE COLLECTION. It's your friend, not your enemy.

Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at for other WebSphere information.

Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at for other WebSphere information.
Rishi Singh
Ranch Hand

Joined: Dec 09, 2000
Posts: 321
"it's much safer to just create new instances in automatic variables and let things be handled that way"..would like some insight on it.If Iam creating new instances for evry thread,it is going to consume my memory resources, so here since I am using java classes to encapsulate the buisness-logic, I would like to instantiate it once and keep it in the memory, to achieve the same functionality which a servlet gives.In that scenario I have to share this object in memory with all my I need to synchronize acess to this object.Is there any other way it can be done.
Secondly, if I am having certain instance variable as part of my java class,how can I rely on one instance to cater to all the threads as seperate requests can have different values for the instances variable .In short I can put it this way how do I achieve the functionality of a Single Threaded Model in a java Object Locking or by Class Locking.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12678
You can keep down the memory overhead by reusing objects, but obviously you have to prevent multi-thread collisions. There are plenty of examples of Database Connection pooling code in Java to use for inspiration - just search the net.
If you are using a "Singleton" pattern for this business logic object you obviously have to use synchronization - it is really not that big a deal.
Don't blindly create new instances if you don't have to - think in terms of page / request / session / application scope and create objects accordingly. Analyze your problem in terms of what data is unique to the user, don't mix up data of different scope in the same class.
Read about Design Patterns for inspiration

author of:

Java Resources at
I agree. Here's the link:
subject: Whether this architecture is flawless!!!
Similar Threads
whether this architecture is the Authors!!!!!
Servlet's constructor
servlet and thread synchronization
Architecture for a web application
When should Servlet utility classes be Servlets and when ordinary Java classes?