gpt4 book ai didi

tomcat - javax.命名.NamingException : Cannot create resource instance

转载 作者:行者123 更新时间:2023-11-28 22:34:46 25 4
gpt4 key购买 nike

关于这个问题似乎有几个主题,但还没有找到答案。我对 JSF 很陌生,想创建一个 postgresql 连接池,使用:

  • JSF 2.2.9
  • Tomcat 8.0.27

但是 tomcat 总是给我一个 HTTP Status 500 - Error instantiating servlet class 错误(帖子末尾的堆栈跟踪)你能帮帮我吗?

我的文件:

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>

上下文.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

最佳答案

多么愚蠢的错误!这只是 context.xml 文件中的错字:

type="javax.sql.Datasource"

改为:

type="javax.sql.DataSource"

现在它工作正常:)

关于tomcat - javax.命名.NamingException : Cannot create resource instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32946616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com