wood burning stoves*
The moose likes Servlets and the fly likes Connection Object pointing to Null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Connection Object pointing to Null" Watch "Connection Object pointing to Null" New topic
Author

Connection Object pointing to Null

Rajesh Vijaya
Ranch Hand

Joined: Oct 18, 2005
Posts: 48
I'm trying to read the intialization parameters from web.xml and try to connect to the database in my servlet init(ServeltConfig config) method
but my connection object is pointing to null in my doPost method:

But when i'm ttrying to connect to DB without reading from web.xml its working fien.You could also see the commented code in that.
Any help in this regard is greatly appreciable.

here is my two files:

1. web.xml

<servlet>

<servlet-name>regProcessServlet</servlet-name>
<servlet-class>RegProcessServlet</servlet-class>
<init-param>
<param-name>DRIVER</param-name>
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
</init-param>
<init-param>
<param-name>URL</param-name>
<param-value>jdbc dbc:mySQLDSN</param-value>
</init-param>
<init-param>
<param-name>USER</param-name>
<param-value>""</param-value>
</init-param>
<init-param>
<param-name>PASSWD</param-name>
<param-value>"" </param-value>
</init-param>
</servlet>

2.RegProcessServlet



[Edit - code tags added by Dave]
[ November 29, 2005: Message edited by: David O'Meara ]

Rajesh <br /> <br />SCJP1.4 SCWCD1.4 SCBCD 1.3 ,SCDJWS(Preparing..)<br /> <br />There is no free will.It is the phenomenon bound by cause and effect.But there is something behind will which is free---Swami Vivekananda...
Sathya Srinivas
Greenhorn

Joined: Nov 29, 2005
Posts: 1
Hi change the following code
<param-name>USER</param-name>
<param-value>""</param-value>
</init-param>
<init-param>
<param-name>PASSWD</param-name>
<param-value>"" </param-value>

as follows
<param-name>USER</param-name>
<param-value></param-value>
</init-param>
<init-param>
<param-name>PASSWD</param-name>
<param-value></param-value>
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Getting DB connection or Database operations should not be done in servlet. Servlet, on the other hand, should only be used as a controller.
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
If a servlet uses database connection, then init method is the best place where to obtain it.


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Are you using MySQL? Why are you using the JDBC-ODBC bridge driver? It's much better to use the MySQL JDBC driver.

The JDBC-ODBC bridge driver is only meant for educational purposes, it is not suited for real world applications. For more info see: JDBC-ODBC Bridge.
[ November 30, 2005: Message edited by: Jesper de Jong ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by dema rogatkin:
If a servlet uses database connection, then init method is the best place where to obtain it.


Its not like that. I am not going into details otherwise it would become off-topic.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by dema rogatkin:
If a servlet uses database connection, then init method is the best place where to obtain it.


This is generally considered a bad idea.
If you obtain your connection in the init method then all requests will have to share it. The connection will become a bottleneck at best and a threading nightmare at worst. There is also the danger of the connection timing out before the servlet is destroyed.

Most conventional wisdom, these days, instructs us to use connection pooling for anything but the most trivial apps.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

We normally like people to justify their opinions with some backing argument, but since this is more of a vote, I also vote for connection pools.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

In my opinion, not using container-managed connection pooling in a web app that uses a database is nothing short of madness.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
Originally posted by Adeel Ansari:


Its not like that. I am not going into details otherwise it would become off-topic.
As you can guess I disagree with you however I agree do not go in details to reduce bandwidth.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Originally posted by dema rogatkin:
As you can guess I disagree with you however I agree do not go in details to reduce bandwidth.


dema, you are wrong. Opening a database connection in the init() method of a servlet and then using it in the servlet is really a bad idea.

Have a look at the following thread if you want a number of reasons why:
http://www.coderanch.com/t/361654/Servlets/java/database-connection-init-servlet
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
Jesper,
I see what you meant. Just curious who were your educators, or those just yours ideas? No need to answer, I simple trying to understand who is behind modern software engineering.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by dema rogatkin:
As you can guess I disagree with you however I agree do not go in details to reduce bandwidth.


As you have already got lots of explaination about this idea and why it is considered bad. I didn't go for reasoning because the thread topic is not about where to get connection or whether its a good idea to get connection in init() method. My intend was not to hijack the main thread rather than saving band-width. But Ben, and other did well. Because it was really necessary to tell you about your idea. Otherwise you might be screwed in future.

Thanks.
[ December 01, 2005: Message edited by: Adeel Ansari ]
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
Originally posted by Adeel Ansari:


As you have already got lots of explaination about this idea and why it is considered bad. I didn't go for reasoning because the thread topic is not about where to get connection or whether its a good idea to get connection in init() method. My intend was not to hijack the main thread rather than saving band-width. But Ben, and other did well. Because it was really necessary to tell you about your idea. Otherwise you might be screwed in future.

Thanks.

[ December 01, 2005: Message edited by: Adeel Ansari ]


Bad thing is that younger developers reading explanation having no any conjuction with real things will gain wrong experience and certainly will be screwed in future. So I'd suggext to stop confuse people.

Thanks,
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

But we have a strong tradition at the JavaRanch of encouraging people to use stable solutions from the beginning, rather than teaching lessons they will have to unlearn in the future.

If this topic is closed we should let it rest, otherwise I will have to delete the last half dozen posts or so. People asking questions have the right have their questions answered without side conversations.

Comments on Connection Pooling, please.

Dave
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by dema rogatkin:
Bad thing is that younger developers reading explanation having no any conjuction with real things will gain wrong experience and certainly will be screwed in future. So I'd suggext to stop confuse people.


Actually, I am a professional web app developer. I have built quite a no. of web apps that are flying in the real world. It was not just my guess. I knew the reasons and could provide you all in a good way.

My intend was not to confuse anybody here, as I already said my intention. I have noticed that just because of this explaination, David thought that this thread is about connection pooling, however it is not.

I have learnt many things here at Ranch. Some of those are:

- Be nice
- Never steer the thread in other direction, because it would irritate the O.P.(Original Poster)
- Dig the person to whom you would like to learn, but in a nicer manner.

I get many emails, PMs from the folks here at Ranch ragarding their problems. I don't mind those. I can explain the things other than topic if O.P. wants me to do that. Hence, it was just a suggestion. If O.P. or someone else has some confusions wiht it and need more explainations then he/she can always PM me, email me, or start a new topic. Moreover, I have already admitted that here I should have given the explaination, as some folks did, in order to provide a detailed reason of my view, or you can say my guess.

Thanks.
[ December 02, 2005: Message edited by: Adeel Ansari ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Originally posted by dema rogatkin:
Jesper,
I see what you meant. Just curious who were your educators, or those just yours ideas? No need to answer, I simple trying to understand who is behind modern software engineering.

I am a professional Java / J2EE developer and I've been writing web applications for a number of different companies for the last six years. I learned most of it by doing Java and J2EE courses, reading books, from colleagues, learning and experimenting in my own time and ofcourse experience in projects for my clients.

Using something like a connection pool isn't my own idea or invention. It's a well-known technique which is used in almost any serious real-world web application. You can find it in any good book about developing web applications.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connection Object pointing to Null
 
Similar Threads
Getting NullPointerException
500 Servlet Exception
Facing Problems with DataBase
SQL syntax error
javax. servlet.Servlet Exception: Initialization failed, Unable to get DB connection