Bo,
Thank you for your advice. Although it is a really good suggestion, it shouldn't make
a difference, it didn't. I'll keep looking into the issue.
Dave
Bo Xu wrote:
> David Bustin wrote:
>
> > Servlet experts,
> >
> > I am learning Java and servlets and ran into a challenge getting a JDBC connection
> >
> > to work within a servlet.
> >
> > I can get the connection to work via a simple java app without any issues. As a
> > servlet
> > I get the errors (see screen shot) below.
> >
> > Using tomcat3.2 on NT4.0 w/ jdk1.3.1 and a Oracle 8.1.6 database on a remote
> > Unix host.
> >
> > What am I doing wrong?
> >
> > Thanks,
> > Dave
> >
> > ======== Screen shot ========
> > it's a DB test
> >
> > Error: 500
> >
> > Location: /examples/servlet/DBServlet
> >
> > Internal Servlet Error:
> >
> > java.lang.NullPointerException
> > at DBServlet.service(DBServlet.java:54)
> > at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
> > at
> > org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405)
> > at org.apache.tomcat.core.Handler.service(Handler.java:287)
> > at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
> > at
> > org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:812)
> > at org.apache.tomcat.core.ContextManager.service(ContextManager.java:758)
> > at
> >
>org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:213)
> >
> > at
> > org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
> > at
> > org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501)
> > at java.lang.Thread.run(Thread.java:484)
> > ======== DBServlet.java ========
> > import java.io.*;
> > import java.sql.*;
> > import javax.servlet.*;
> > import javax.servlet.http.*;
> >
> > public class DBServlet extends HttpServlet {
> > private Connection con;
> > private PrintWriter out;
> >
> > public void init(ServletConfig conf)
> > throws ServletException {
> > super.init(conf);
> > try{
> >
> > // tried this - statement works within java app but not as servlet
> > //DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
> >
> > // compiles w/o error but gives null pointer exception
> > // on "Statement stmt = con.createStatement();" below
> > Class.forName ("oracle.jdbc.driver.OracleDriver");
> >
> > con = DriverManager.getConnection("jdbc:oracle:thin:@myhost:1521:mySID",
> > "user", "passwd");
> >
> > } catch(Exception e) {
> > System.err.println("Error was: " + e);
> > }
> > }
> >
> > public void service(HttpServletRequest req,
> > HttpServletResponse res)
> > throws ServletException, IOException {
> > res.setContentType("text/html");
> > try {
> > out = res.getWriter();
> > out.println("<html>");
> > out.println("<head>");
> > out.println("<title>DB Demo</title>");
> > out.println("</head>");
> > out.println("<body>");
> >
> > out.println("<h1> it's a DB test </h1>");
> >
> > Statement stmt = con.createStatement();
> > ResultSet rs = stmt.executeQuery("select key from keytable");
> >
> > out.println("<UL>");
> >
> > while(rs.next()) {
> > out.println("<LI>" + rs.getString("key"));
> > }
> >
> > out.println("</UL>");
> > rs.close();
> >
> > stmt.close();
> >
> > } catch(SQLException e) {
> > out.println("An SQL Exception was thrown.");
> > } catch(IOException e) {
> > System.err.println("An IOException was thrown.");
> > }
> >
> > out.println("</body>");
> > out.println("</html>");
> > out.close();
> > }
> >
> > public void destroy(){
> > try{
> > con.close();
> > } catch(SQLException e) {
> > ;
> > }
> > }
> > }
> > [...]
>
> It is better to make out( a PrintWriter) be a local field inside your service
>method, this
> will increase the Thread-safty when more than one request reach your Servlet, but I
>don't
> know if it is the reason of the "java.lang.NullPointerException at
>DBServlet.service(DBServlet.java:54)"
>
> Bo
> Oct.25, 2001
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html