Je vois que cette question a déjà été soulevée à plusieurs reprises et que je les ai toutes passées en revue. Mais je suis toujours incapable de résoudre mon problème.
Quelqu'un pourrait-il m'aider à identifier ce que je fais mal?
Je reçois le message d'erreur suivant lorsque j'essaie d'accéder à database.jsp:
HTTP Status 500 - An exception occurred processing JSP page /database.jsp at line 24
type Exception report
message An exception occurred processing JSP page /database.jsp at line 24
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.Apache.jasper.JasperException: An exception occurred processing JSP page /database.jsp at line 24
21: <%
22: Context init = new InitialContext();
23: Context env = (Context) init.lookup("Java:/comp/env/");
24: DataSource data = (DataSource) env.lookup("jdbc/mydb");
25: Connection conn = data.getConnection();
26: Statement stmt = conn.createStatement();
27:
Stacktrace:
org.Apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.Java:568)
org.Apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.Java:455)
org.Apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.Java:390)
org.Apache.jasper.servlet.JspServlet.service(JspServlet.Java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.Java:727)
org.Apache.Tomcat.websocket.server.WsFilter.doFilter(WsFilter.Java:52)
root cause
javax.servlet.ServletException: javax.naming.NameNotFoundException: Name [jdbc/mydb] is not bound in this Context. Unable to find [jdbc].
org.Apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.Java:916)
org.Apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.Java:845)
org.Apache.jsp.database_jsp._jspService(database_jsp.Java:185)
org.Apache.jasper.runtime.HttpJspBase.service(HttpJspBase.Java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.Java:727)
org.Apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.Java:432)
org.Apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.Java:390)
org.Apache.jasper.servlet.JspServlet.service(JspServlet.Java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.Java:727)
org.Apache.Tomcat.websocket.server.WsFilter.doFilter(WsFilter.Java:52)
Voici quelques informations sur mon installation: Mon ordinateur fonctionne sous Mac OS X. Le serveur Web est Tomcat version 7.0.52, installé sous /Users/username/Dropbox/Server/Apache-Tomcat-7.0.52. J'essaie d'exécuter Derby 10.10.1.1 en tant que base de données intégrée. J'ai copié le fichier derby.jar sous le dossier /Users/nom_utilisateur/Dropbox/Server/Apache-Tomcat-7.0.52/lib. J'ai écrit ce qui suit dans mon fichier PATH (je ne suis pas sûr que ce soit pertinent):
export DERBY_HOME=/Users/username/Dropbox/Server/Apache-Tomcat-7.0.52/lib
export PATH=$PATH:$DERBY_HOME/bin
Voici le contenu du fichier web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://Java.Sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://Java.Sun.com/xml/ns/javaee
http://Java.Sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mydb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</description>
</web-app>
context.xml
<?xml version='1.0' encoding='utf-8'?>
<!-- The contents of this file will be loaded for each web application -->
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
server.xml
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.Apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.Apache.catalina.core.JasperListener" />
<Listener className="org.Apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.Apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.Apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<!-- Added by LC #2 @19/3/2014 -->
<!-- Global Datasource for Derby mydb database -->
<Resource name="jdbc/mydb"
type="javax.sql.DataSource" auth="Container"
description="Derby database for Stocktails"
maxActive="20" maxIdle="10" maxWait="-1"
username="" password=""
driverClassName="org.Apache.derby.jdbc.EmbeddedDriver"
url="jdbc:derby:/Users/username/Dropbox/Server/Apache-Tomcat-7.0.52/databases/mydb"/>
<!-- End #2 -->
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.Apache.catalina.realm.LockOutRealm">
<Realm className="org.Apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.Apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<!-- Added by LC #1 @19/3/2014 -->
<Context
docBase="ROOT" path="" debug="0"
reloadable="true">
</Context>
<!-- End #1 -->
</Host>
</Engine>
</Service>
</Server>
Enfin, voici la page database.jsp:
<html>
<head>
<title>Quote Query</title>
</head>
<body>
<h1>retrieve stock quotes</h1>
<h3>Choose Ticker(s):</h3>
<form method="get">
<input type="checkbox" name="ticker" value="AAPL">AAPL
<input type="checkbox" name="ticker" value="MSFT">MSFT
<input type="submit" value="Query">
</form>
<%
String[] tickers = request.getParameterValues("ticker");
if (tickers != null) {
%>
<%@ page import="javax.naming.*,Java.sql.*,javax.sql.*" %>
<%
Context init = new InitialContext();
Context env = (Context) init.lookup("Java:/comp/env/");
DataSource data = (DataSource) env.lookup("jdbc/mydb");
Connection conn = data.getConnection();
Statement stmt = conn.createStatement();
String sqlStr = "SELECT * FROM Quotes WHERE Ticker IN (";
sqlStr += "'" + tickers[0] + "'"; // First ticker
for (int i = 1; i < tickers.length; ++i) {
sqlStr += ", '" + tickers[i] + "'"; // Subsequent tickers need a leading commas
}
sqlStr += ") ORDER BY Date ASC";
// for debugging
System.out.println("Query statement is " + sqlStr);
ResultSet rset = stmt.executeQuery(sqlStr);
%>
<hr>
<form method="get" action="quotes.jsp">
<table border=1 cellpadding=5>
<tr>
<th>Ticker</th>
<th>Date</th>
<th>PriceOpen</th>
<th>PriceHigh</th>
<th>PriceLow</th>
<th>PriceClose</th>
<th>Volume</th>
<th>PriceAdjClose</th>
</tr>
<%
while (rset.next()) {
int id = rset.getInt("id");
%>
<tr>
<td><input type="checkbox" name="id" value="<%= id %>"></td>
<td><%= rset.getString("Ticker") %></td>
<td><%= rset.getString("Date") %></td>
<td>$<%= rset.getInt("PriceOpen") %></td>
<td><%= rset.getInt("PriceHigh") %></td>
<td><%= rset.getString("PriceLow") %></td>
<td><%= rset.getString("PriceClose") %></td>
<td>$<%= rset.getInt("Volume") %></td>
<td><%= rset.getInt("PriceAdjClose") %></td>
</tr>
<%
}
%>
</table>
<br>
<input type="submit" value="Order">
<input type="reset" value="Clear">
</form>
<a href="<%= request.getRequestURI() %>"><h3>Back</h3></a>
<%
rset.close();
stmt.close();
conn.close();
}
%>
</body>
</html>
Bûche
Mar 21, 2014 7:57:26 AM org.Apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the Java.library.path: /Users/lcazarre/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/Java:.
Mar 21, 2014 7:57:26 AM org.Apache.Tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'debug' to '0' did not find a matching property.
Mar 21, 2014 7:57:28 AM org.Apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Mar 21, 2014 7:57:28 AM org.Apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Mar 21, 2014 7:57:28 AM org.Apache.catalina.startup.Catalina load
INFO: Initialization processed in 3227 ms
Mar 21, 2014 7:57:28 AM org.Apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 21, 2014 7:57:28 AM org.Apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.52
Mar 21, 2014 7:57:28 AM org.Apache.catalina.realm.UserDatabaseRealm startInternal
SEVERE: Exception looking up UserDatabase under key UserDatabase
javax.naming.NameNotFoundException: Name [UserDatabase] is not bound in this Context. Unable to find [UserDatabase].
at org.Apache.naming.NamingContext.lookup(NamingContext.Java:819)
at org.Apache.naming.NamingContext.lookup(NamingContext.Java:167)
at org.Apache.catalina.realm.UserDatabaseRealm.startInternal(UserDatabaseRealm.Java:253)
at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
at org.Apache.catalina.realm.CombinedRealm.startInternal(CombinedRealm.Java:201)
at org.Apache.catalina.realm.LockOutRealm.startInternal(LockOutRealm.Java:120)
at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
at org.Apache.catalina.core.ContainerBase.startInternal(ContainerBase.Java:1109)
at org.Apache.catalina.core.StandardEngine.startInternal(StandardEngine.Java:300)
at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
at org.Apache.catalina.core.StandardService.startInternal(StandardService.Java:443)
at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
at org.Apache.catalina.core.StandardServer.startInternal(StandardServer.Java:731)
at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
at org.Apache.catalina.startup.Catalina.start(Catalina.Java:689)
at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:57)
at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
at Java.lang.reflect.Method.invoke(Method.Java:606)
at org.Apache.catalina.startup.Bootstrap.start(Bootstrap.Java:321)
at org.Apache.catalina.startup.Bootstrap.main(Bootstrap.Java:455)
Mar 21, 2014 7:57:28 AM org.Apache.catalina.realm.CombinedRealm startInternal
SEVERE: Failed to start "org.Apache.catalina.realm.UserDatabaseRealm/1.0" realm
org.Apache.catalina.LifecycleException: Failed to start component [Realm[UserDatabaseRealm]]
at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:154)
at org.Apache.catalina.realm.CombinedRealm.startInternal(CombinedRealm.Java:201)
at org.Apache.catalina.realm.LockOutRealm.startInternal(LockOutRealm.Java:120)
at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
at org.Apache.catalina.core.ContainerBase.startInternal(ContainerBase.Java:1109)
at org.Apache.catalina.core.StandardEngine.startInternal(StandardEngine.Java:300)
at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
at org.Apache.catalina.core.StandardService.startInternal(StandardService.Java:443)
at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
at org.Apache.catalina.core.StandardServer.startInternal(StandardServer.Java:731)
at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
at org.Apache.catalina.startup.Catalina.start(Catalina.Java:689)
at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:57)
at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
at Java.lang.reflect.Method.invoke(Method.Java:606)
at org.Apache.catalina.startup.Bootstrap.start(Bootstrap.Java:321)
at org.Apache.catalina.startup.Bootstrap.main(Bootstrap.Java:455)
Caused by: org.Apache.catalina.LifecycleException: No UserDatabase component found under key UserDatabase
at org.Apache.catalina.realm.UserDatabaseRealm.startInternal(UserDatabaseRealm.Java:262)
at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
... 17 more
Mar 21, 2014 7:57:31 AM org.Apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /Users/lcazarre/Dropbox/Work/MS50/COMSM0104/Server/Apache-Tomcat-7.0.52/webapps/docs
Mar 21, 2014 7:57:32 AM org.Apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /Users/lcazarre/Dropbox/Work/MS50/COMSM0104/Server/Apache-Tomcat-7.0.52/webapps/examples
Mar 21, 2014 7:57:33 AM org.Apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /Users/lcazarre/Dropbox/Work/MS50/COMSM0104/Server/Apache-Tomcat-7.0.52/webapps/Host-manager
Mar 21, 2014 7:57:33 AM org.Apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /Users/lcazarre/Dropbox/Work/MS50/COMSM0104/Server/Apache-Tomcat-7.0.52/webapps/manager
Mar 21, 2014 7:57:34 AM org.Apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Mar 21, 2014 7:57:34 AM org.Apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Mar 21, 2014 7:57:34 AM org.Apache.catalina.startup.Catalina start
INFO: Server startup in 6321 ms
Merci, LC
Vous avez besoin d’un ResourceLink dans votre META-INF/context.xml
fichier pour rendre la ressource globale disponible pour l'application Web.
<ResourceLink name="jdbc/mydb"
global="jdbc/mydb"
type="javax.sql.DataSource" />
Pour ceux qui utilisent Tomcat avec Bitronix, cela résoudra le problème:
L'erreur indique qu'aucun gestionnaire n'a pu être trouvé pour votre source de données 'jdbc/mydb', vous devez donc vous assurer que votre serveur Tomcat fait référence à vos fichiers de configuration bitronix selon les besoins.
Si vous utilisez des fichiers btm-config.properties et resources.properties pour configurer la source de données, spécifiez ces deux arguments JVM dans Tomcat:
(si vous les avez déjà utilisées, assurez-vous que vos références sont correctes):
par exemple.
-Dbtm.root="C:\Program Files\Apache Software Foundation\Tomcat 7.0.59"
-Dbitronix.tm.configuration="C:\Program Files\Apache Software Foundation\Tomcat 7.0.59\conf\btm-config.properties"
Maintenant, redémarrez votre serveur et consultez le journal.