aspose file tools*
The moose likes Servlets and the fly likes If no instance variables, how to init variables in seperate methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "If no instance variables, how to init variables in seperate methods" Watch "If no instance variables, how to init variables in seperate methods" New topic
Author

If no instance variables, how to init variables in seperate methods

Kenny Dilger
Greenhorn

Joined: Jan 14, 2002
Posts: 11
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
Asher Tarnopolski
Ranch Hand

Joined: Jul 28, 2001
Posts: 260
i think your conclusion is wrong.
you can use instance variables, but you should be aware of the need of synchronization. you can also use SingleThreadModel interface...


Asher Tarnopolski
SCJP,SCWCD
Kenny Dilger
Greenhorn

Joined: Jan 14, 2002
Posts: 11
Okay, what is the proper way of doing this then? I have read lots of posts saying to not use any instance variables. Here you state that instance variables can be used if they are synchronized.
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kenny Dilger
Greenhorn

Joined: Jan 14, 2002
Posts: 11
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
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
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
 
 
subject: If no instance variables, how to init variables in seperate methods