Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Kenny Dilger
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Kenny Dilger
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Kenny Dilger
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic