简体   繁体   中英

Tomcat6 and MySQL issues

I have a new installation of Ubuntu 11.10 server with Tomcat6. I am using Eclipse Indigo to make a Java Servlet that pulls data from my MySQL server. I have placed the latest Connector/J (mysql-connector-java-5.1.18-bin.jar) in my servers CATALINA_HOME/lib directory. In Eclipse I make a Dynamic Web Project to which I then create a servlet. The Java code for the servlet is:

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

/**
 * Servlet implementation class TestServlet
 */
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public TestServlet() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    Context initCtx = null;
    try {
        initCtx = new InitialContext();
    } catch (NamingException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    Context envCtx = null;
    try {
        envCtx = (Context) initCtx.lookup("java:comp/env");
    } catch (NamingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    DataSource ds = null;
    try {
        ds = (DataSource) envCtx.lookup("jdbc/TestDB");
    } catch (NamingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    Connection conn = null;
    try {
        conn = ds.getConnection();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Statement stat = null;
    try {
        stat = conn.createStatement();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ResultSet rs = null;
    try {
        rs = stat.executeQuery("SELECT * FROM users");
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        while(rs.next()) {
            System.out.println("in");
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

I have the following in my WEB-INF/web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>TestServlet</display-name>
  <resource-ref>
    <description>TestDB</description>
    <res-ref-name>jdbc/TestDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>TestServlet</display-name>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/TestServlet</url-pattern>
  </servlet-mapping>
</web-app>

I had to create the context.xml in META-INF, but here is the context of my META-INF/context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
  <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="myuser" password="mypass" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/test1"/>
</Context>

I export to a war, deploy to my tomcat server. When I browse to the servlet I get the following error:

java.lang.NullPointerException
TestServlet.doGet(TestServlet.java:67)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

Line 67 of TestServlet.java is the conn.createStatement(); line. Sorry my code is messy, I went with individual try catch blocks because I don't get this error when I do one big try catch, though it still does not connect to the MySQL database. I'm sure I am missing a rather simple step here, but I can't seem to find it anywhere, I'm a bit of a newb with Tomcat and Servlets. Thanks a ton.

As per the Tomcat 6 context docs :

Context elements may be explicitly defined:

  • Only if a context file does not exist for the application in the $CATALINA_BASE/conf/[enginename]/[hostname]/, in an individual file at /META-INF/context.xml inside the application files. If the web application is packaged as a WAR then /META-INF/context.xml will be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to match the application's context path. Once this file exists, it will not be replaced if a new WAR with a newer /META-INF/context.xml is placed in the host's appBase.

Try deleting the copy in Tomcat's conf dir and see if your context.xml file is then picked up.

The problem turned out to be in my /etc/mysql/my.conf . I had set my bind-address to the ip address of the server, this was causing the inability for Tomcat to connect. Why Tomcat can't but PHP can is beyond me, but whatever. I changed my bind-address to 0.0.0.0 and all is well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM