This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Dear all, I have the following doubts: First: 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 etc...) 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. Second: 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)? Third: 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 I WOULD APPRECIATE A DETAILED RESPONSE FROM THE SHERRIFS AND ANYONE WHO IS DWINDLING WITH WEB TECHNOLOGIES AND HAVE ENOUGH EXPERIENCE...I would like to know where would this approach go haywire.
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
Kylie, "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 threads..so 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 class....by Object Locking or by Class Locking.
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 Bill