aspose file tools*
The moose likes Servlets and the fly likes init() questions - confused Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "init() questions - confused" Watch "init() questions - confused" New topic
Author

init() questions - confused

Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
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


SCJP 1.4, www.gsi3d.org.uk
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Aug 14, 2001
Posts: 342
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!
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
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

Joined: Aug 14, 2001
Posts: 342
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

Joined: Dec 13, 2004
Posts: 429
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
 
Consider Paul's rocket mass heater.
 
subject: init() questions - confused