1

Tomcat 7 has the jar files commons-dbcp.jar and tomcat-dbcp.jar but in tomcat-dbcp.jar, it is the file org.apache.tomcat.dbcp.BasicDataSourceFactory class instead of org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory that is looking for.

root cause

javax.naming.NamingException: Could not create resource factory instance [Root exception is java.lang.ClassNotFoundException: org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory]
    org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:121)
    javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
    org.apache.naming.NamingContext.lookup(NamingContext.java:843)
    org.apache.naming.NamingContext.lookup(NamingContext.java:154)
    org.apache.naming.NamingContext.lookup(NamingContext.java:831)
    org.apache.naming.NamingContext.lookup(NamingContext.java:168)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    java.lang.Thread.run(Thread.java:722)

Servlet code

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

import javax.annotation.Resource;
import javax.sql.DataSource;

@WebServlet("/person.do")
public class PersonServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Resource(name = "jdbc/javalabs")
    DataSource ds;

    public PersonServlet() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            Connection con = ds.getConnection();

            Statement stmt = con.createStatement();
            String query = "select * from t_person";
            ResultSet rs = stmt.executeQuery(query);

            PrintWriter out = response.getWriter();
            response.setContentType("text/html");
            out.print("<center><h1>Person Details</h1></center>");
            out.print("<html><body>");
            out.print("<table border=\"1\" cellspacing=10 cellpadding=5>");
            out.print("<tr><th>ID</th>");
            out.print("<th>First Name</th>");
            out.print("<th>Last Name </th>");
            out.print("<th>Date of birth</th></tr>");

            while (rs.next()) {
                out.print("<tr>");
                out.print("<td>" + rs.getLong("id") + "</td>");
                out.print("<td>" + rs.getString("firstName") + "</td>");
                out.print("<td>" + rs.getString("lastName") + "</td>");
                out.print("<td>" + rs.getDate("dob") + "</td>");
                out.print("</tr>");
            }
            out.print("</table></body></html>");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }
}

META-INF/context.xml

<?xml version="1.0" encoding="UTF-8"?>
 <!-- The contents of this file will be loaded for each web application -->
  <Context crossContext="true">

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

   <Resource name="jdbc/testDB" auth="Container"
        type="javax.sql.DataSource"
        maxActive="100" maxIdle="30" maxWait="10000"
        username="myuser"
        password="myuser"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/javalabs"/>
</Context>

Element in WEB-INF/web.xml

  <resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/testDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
4

0 回答 0