• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

init() questions - confused

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I want to use init() to do some stuff with config and context init-params in my servlet. One of the things I want to do is open a DB connection, which I believe is quite a common use of init().

Anyway, I am learning about all this stuff by reading HF Servlets and JSP, in which I seem to remember it saying that instance variables in servlets are a bad idea. I only actually _need_ the DB connection briefly in order to read some configuration data in, so I could actually keep that object method local within init(). However, I would like to keep a copy of the connection parameters so I can display them in a JSP later, but the only way my small brain can see to do this is to store them in an instance variable (like HashMap). I can't, for example, do



because at the init() stage there is no context!

Can anyone suggest what to do here? Where can I store these params for later without using an instance variable? Do I even need to? I'm confused

thanks,
Ben
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a context at the init stage.

Another approach for initializing your app is would be to put that code in a contextListener.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContextListener.html

This way, you know your initialization will be completed before any servlets/JSP are run.

Also, if this is to be a production app, I wouldn't create a connection in the init method and force all the requests to share it. Look into your app servers connection pooling capabilities.
 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ben. Your advice is much appreciated.

The reason I didn't think I had a context during init() was because doing so caused a null pointer exception. I have now realised that the reason for this was that I had overridden init(ServletConfig) instead of just init(). Doh!

I had thought of a context listener, but need to read up on that a little more! That may be the way I go ultimately.

Yes this will be a production app, but the database connection is not actually used by the service methods. It is simply used to load some extra, more complex, config data during init() which I don't think is appropriate to place in the deployment descriptor. Also, by keeping this data in the DB, rather than in the DD, that allows me to refresh that info without restarting the app by asking it to re-read the DB using some kind of refresh request. Do you think this sounds appropriate.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does and I do the same thing (from a context listener).

I've seen some tutorials that create a DB connection in the init method, use it in all of the service methods, and then close it in the destroy method.
This is what I was warning against.

Sounds like you're on the right track.
 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers Ben. All this stuff is taking a while to sink in. Most of my Java before was back-end stuff with the odd bit of lame servlet-ing now and then, but I'm really trying to get up to speed on the web side now - there is much to learn!
 
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I only actually _need_ the DB connection briefly in order to read some configuration data in, so I could actually keep that object method local within init(). However, I would like to keep a copy of the connection parameters so I can display them in a JSP later, but the only way my small brain can see to do this is to store them in an instance variable (like HashMap).

That's exactly what I would do. It might be dangerous to keep a live Connection object around, but there is no problem with having instance variables as long as you don't modify them after the end of init(). It doesn't matter if a thousand threads concurrently access the same HashMap as long as none of them modify it.

-Yuriy
[ August 19, 2005: Message edited by: Yuriy Zilbergleyt ]
 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Yuriy. The way I have this now is to store my configuration stuff into a HashMap and put that into the context as an attribute:



...that way I can use it with EL in my JSP's to interrogate the servlet to find out where its getting its data from and display it in the browser without having to open the web.xml! For example...



...meanwhile the servlet will continue to serve its data to requests coming in via doPost().

Getting there ... slowly!
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That works. Just remember that when you decide to refresh the values from the database, you need to create a new Map and replace the old one with it. Do NOT modify the old one, as some JSPs might be accessing it at the moment.

Good luck!

-Yuriy
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic