javax.naming.NamingException:无法创建资源实例

时间:2015-10-05 10:41:18

标签: tomcat exception servlets jndi

似乎有关于这个问题的几个主题,但还没有找到答案。我是JSF的新手,想要使用以下方法创建 postgresql连接池

  • JSF 2.2.9
  • Tomcat 8.0.27

但是tomcat总是给我一个 HTTP状态500 - 错误实例化servlet类错误(帖子末尾的堆栈跟踪) 你能救我吗?

我的档案:

的web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
      <display-name>Postgresql</display-name>
      <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
      </servlet-mapping>
      <context-param>
        <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
      </context-param>
      <context-param>
        <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
        <param-value>resources.application</param-value>
      </context-param>
      <listener>
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
      </listener>
      <resource-ref>
        <description>Postgres Test</description>
        <res-ref-name>jdbc/einfuehrungsaufgabe</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
      </resource-ref>
    </web-app>

的context.xml

<context>

    <Resource name="jdbc/einfuehrungsaufgabe" auth="Container"
        type="javax.sql.Datasource" maxActive="20" maxIdle="5" maxWait="10000"
        username="foo" password="foo" driverClassName="org.postgresql.Driver"
        url="jdbc:postgresql://foo.de/foo">
    </Resource>

</context>

(更改了数据库地址,用户名和密码)

TestServlet.java

package com.postgres;

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

import javax.annotation.Resource;
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 javax.sql.DataSource;

@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = -7524281845027879453L;
    @Resource(name = "jdbc/einfuehrungsaufgabe")
    private DataSource dataSource;


    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        response.setContentType("text/plain");

        Connection myConn = null;
        Statement myStmt = null;
        ResultSet myRs = null;

        //some code
    }

}

堆栈跟踪

javax.servlet.ServletException: Error instantiating servlet class com.postgres.TestServlet
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)
root cause

javax.naming.NamingException: Cannot create resource instance
    org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:96)
    javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
    org.apache.naming.NamingContext.lookup(NamingContext.java:841)
    org.apache.naming.NamingContext.lookup(NamingContext.java:152)
    org.apache.naming.NamingContext.lookup(NamingContext.java:829)
    org.apache.naming.NamingContext.lookup(NamingContext.java:166)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)

WEB-INF / lib中

  • 的javax.servlet-API-3.1.0.jar
  • 的PostgreSQL-9.4-1203.jdbc4.jar

2 个答案:

答案 0 :(得分:4)

多么愚蠢的错误! 这只是context.xml文件中的一个拼写错误:

type="javax.sql.DataSource"

更改为:

replaced = re.sub(r'(<h1>)(.*?)(</h1>)', r'<h2>\2</h2>', s)

现在它可以正常工作:)

答案 1 :(得分:0)

您可以使用以下代码在context.xml的{​​{1}}中进行资源配置:

WebContent/META-INF