This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
From reading through a lot of threads I have come to the conclusion that instance variables should not be used because in the Servlet model only on Instance of the Servlet class will exist. So, if instance variables can't be used how are multiple objects/data passed into a method and then in that method when they Objects are initialized used in the doGet and doPost methods. Basically I have some connection routines to an IMAP mailbox that will happen more than once. I want to put that part into a connect_mailbox method that takes a Folder and a Store. How can the Folder and Store be used else where if they are not instance variables? I have some ideas but am looking for the correct way... Thanks, Kenny
SingleThreadModel is generally not a good option. This increases the overhead on your server and won't keep a connection constant for your user across multiple requests anyway. Also keep in mind that you cannot synchronize a particular object (i.e. variable), only their methods. The proper means of doing this is first to determine if it's really necessary to cache these objects. What is the connection overhead? Will connections expire? Will they be shared? If connections can be safely shared (i.e. they are themselves thread-safe), you can safely keep them as instance variables. Or better still, keep a connection pool of them available. On the other hand, if overhead is low and connections can expire anyway, don't bother trying to keep them and track them. If you really feel you need to maintain them, you can store them in the user's session using the methods available in the HttpServletRequest and HttpSession interfaces. Items that will be used across multiple requests will need stored in the session. Items simply used in the request scope, obviously, get stored in the request. As a hint, look for the getAttribute and setAttribute methods.
"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
Instance variables can be used if judiciously synchronixed, but I don't see it done much because it's just too much hassle and too error prone to be worth it. I don't ever use them in a servlet and don't recommend them. Since all servlet processing happens from a few single points of entry (one of doPost(), doGet()), the most common means of intra-method communciations for request-specific information is to simply pass things around as parameters. For more persistent items (like connection pools) there are the attributes you can set at various levels (application, session, request) according to the scope you need. hth, bear [ January 21, 2003: Message edited by: Bear Bibeault ]
Using HttpSession was one of my thoughts but I was not sure if that is how it should be done. Basically what I am doing is wanting to have an instance of IMAPFolder and IMAPStore available so I do not have to re-initialize the connection to the mail server all the time... Should I not do it this way. Would it be better to just re-init them everytime I need them? Thanks! Kenny
The simplest thing would be to get your application running by creating connections as needed for each request. Just be sure you properly close the connections. If it turns out that is too slow, you can always go to some sort of managed connection pool - use the code for the PooledConnection, etc. in the javax.sql package as an example. Bill