wood burning stoves*
The moose likes Servlets and the fly likes init(ServletConfig) not being called Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "init(ServletConfig) not being called" Watch "init(ServletConfig) not being called" New topic
Author

init(ServletConfig) not being called

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15290
    
    6

Does the init method always get called in a Servlet or only when there is a doPost method. I have a doGet method and my init method is not getting called. Is there a way to force it to be called every time?

If I move the code in init() to the doGet method, it works great. I am getting a NullPointerException at the stmt = connection.createStatement() line.
Thanks.
[ November 29, 2002: Message edited by: Gregg Bolinger ]

GenRocket - Experts at Building Test Data
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
The API guarantees that init will get called as the first thing that happens after the servlet instance is constructed. Your problem must lie elsewhere.
I like to always have a System.out.println early in the init method.
Bill
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15290
    
    6

I did read that init() gets called when the servlet is instantiated by the webserver (TOMCAT). So is it possible that my Connection object is being GC'd or closed somewhere else?
Should I be handleing a database connection some other way? This was the example given on this web page.
Thanks.
[ November 29, 2002: Message edited by: Gregg Bolinger ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
You should be handling the database connection via a connection pool or creating as needed on a per request basis.
One (of many) reasons not to try to create the connection in init is that the database may decide to close the connection at any time. Remember, a servlet may stay running for weeks or more - everything in a servlet should be designed to reflect this. Connection pools will automatically handle re-creating a connection if the db closes one.
Bill
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15290
    
    6

Thanks Bill. Makes me wonder why Sun decided to show an example using the Connection in the init() method. They also state that the init method is used for initializing components for the Servlet and they even state Connections for Databases.

Thanks for the response though, and I think I will follow your advise.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
That sounds like a surprisingly sloppy example alright. However, note that you should use the init method to get the proper driver loaded as in the first part of your example. That way you discover right away if the driver exists.
If using a connection pool, you would also initialize it in the init method.
Bill
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15290
    
    6

Sounds good Bill. Thanks a ton!
I am still curious though as to why I am getting the nullPointerException. There is probably about a 30 second delay between when I reload the servlet and then run my test program. It is hard for me to believe that the connection is being dropped that fast. If anyone has any insight on this, that would be great.
Thanks.
Kees van Oosterhout
Ranch Hand

Joined: Jul 08, 2002
Posts: 34
I think a servlet should not maintain any state. Who knows how many instances of your servlet are in the container. Try to store your connection in the servlet context. I think this will work fine. You can recall the connection in the doGet() method. Just to be shure, you can display the connection just after creating it.....
Hope it helps....


"...you've got to ask yourself one question, Do I feel lucky?"
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15290
    
    6

Thanks Kees. However, that still doesn not explain why the example from Sun does not work. What is happening to my connection object in the init method that causes it to be null 30 seconds or less after it is created?
Is the example just wrong? I can buy that. But I would like to know why.
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: init(ServletConfig) not being called
 
Similar Threads
servlet-jdbc - internal servlet error (error:500)
Problem with DbConnectionBroker
servlet-jdbc help!!!!!!!!!!!!!!!
IllegalStateException error
Connection Pooling example appears to create more than one pool