我尝试在Eclipse Project中为Jetty 6 Server配置数据源。
我将这些jar包含在我的lib文件夹中:
common-dbcp-1.4.jar
commons-pool-1.5.6.jar
jetty-naming-6.1.14.jar
the jetty-plus-6.1.11.jar
以下是来自WEB-INF的jetty-env.xml
:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure id="myapp" class="org.mortbay.jetty.webapp.WebAppContext">
<New id="DSTest" class="org.mortbay.jetty.plus.naming.EnvEntry">
<Arg><Ref id="myapp" /></Arg>
<Arg>jdbc/DSTest</Arg>
<Arg>
<New class="org.apache.commons.dbcp.BasicDataSource">
<Set name="driverClassName">org.postgresql.Driver</Set>
<Set name="url">dbc:postgresql://localhost:5432/schoeneberg</Set>
<Set name="username">postgres</Set>
<Set name="password">deluxe</Set>
</New>
</Arg>
</New>
</Configure>
来自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" version="2.5">
<resource-ref>
<description>My DataSource Reference</description>
<res-ref-name>jdbc/DSTest</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>de.test.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
这里是Servlet代码:
@SuppressWarnings("serial")
public class Test extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
Context env = (Context) new InitialContext().lookup("java:comp/env/jdbc/DSTest");
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
似乎是Jetty 6中数据源和连接池的简单示例。但我总是得到这个例外:
javax.naming.NameNotFoundException; remaining name 'env/jdbc/DSTest'
at org.mortbay.naming.NamingContext.lookup(NamingContext.java:634)
at org.mortbay.naming.NamingContext.lookup(NamingContext.java:665)
at org.mortbay.naming.NamingContext.lookup(NamingContext.java:680)
at org.mortbay.naming.java.javaRootURLContext.lookup(javaRootURLContext.java:112)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at de.test.Test.doGet(Test.java:25)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:829)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:513)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
有人有想法吗?
感谢您的帮助!
答案 0 :(得分:0)
只使用资源名称:
final Context ctx = new InitialContext();
final DataSource ds = (DataSource) ctx.lookup("jdbc/DSTest");
这对我有用