javax.servlet.ServletException: Name jdbc is not bound in this Context
上传到空间后,没有办法修改tmcat_home下的server.xml文件,所以在我自己目录下增加META-INF/context.xml文件,和WEB-INF/web.xml来配置。
Context.xml
<?xml version="1.0" encoding="GBK"?><!--Context path="/" docBase="" debug="5" reloadable="true" crossContext="true"-->
<Context>
<Resource
auth="Container"
description="test"
name="jdbc/connectDB"
type="javax.sql.DataSource"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
maxIdle="2"
maxWait="5000"
username="sa"
password="pwd"
url="jdbc:microsoft:sqlserver://(主机IP):1433;DatabaseName=db"
maxActive="4"/>
</Context>Web.xml
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd"><web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <description>
Welcome to Tomcat
</description>
<resource-ref>
<description>connectDB</description>
<res-ref-name>jdbc/connectDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth> </resource-ref>
<servlet>
<servlet-name>regservlet</servlet-name>
<display-name>regservlet</display-name>
<description>regservlet</description>
<servlet-class>cn.regservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>regservlet</servlet-name>
<url-pattern>/servlet/regservlet</url-pattern>
</servlet-mapping>
</web-app>测试JSP文件
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.naming.*"%>
<html >
<body >
<%
Context initctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)initctx.lookup("java:comp/env/jdbc/connectDB");
Connection conn = ds.getConnection();
Statement stmt=conn.createStatement();
String sql="select * from AdminInfo";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
out.print("数据库操作成功,恭喜你");
<%}
rs.close();
stmt.close();
conn.close();
%>
</body >
</html >
运行JSP文件,出错。
解决方案 »
- myeclipse启动后 第一次访问页面 mysql进程会自动关闭
- Spring+Hibernate无法向数据库插入数据
- 大家看看这个投票网站的“防同一ip多次投票”的技术原理是什么?
- 点击退出网站后网页怎么跳转到登陆页面?
- JSTL 变量修改
- 关于JAVA监听器监听数据库表的问题
- java连接Access数据库问题?(大家都来顶一下啊!)
- 很奇怪的vector
- java.net.SocketException:Connect Reset 是什么意思呢??有没有高人帮我回答一下????
- 请问如何在用smartupload上传文件时,在文件保存在硬盘以前,给文件更名?焦急中!
- 关于jfreechart的配置问题
- 重用hibernate问题
exception
org.apache.jasper.JasperException: Exception in JSP: /2.jsp:2118: //Object obj = (Object)ctx.lookup("jdbc/connectDB");
19: //out.print(obj);
20: //DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/connectDB");
21: javax.sql.DataSource ds = (javax.sql.DataSource)initctx.lookup("java:comp/env/jdbc/connectDB");
22: //out.print(ds);
23: Connection conn = ds.getConnection();
24: //out.print(conn);
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:467)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:371)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:121)
root cause
javax.servlet.ServletException: Name jdbc is not bound in this Context
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:846)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
org.apache.jsp._2_jsp._jspService(_2_jsp.java:116)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:121)
root cause
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
org.apache.naming.NamingContext.lookup(NamingContext.java:770)
org.apache.naming.NamingContext.lookup(NamingContext.java:140)
org.apache.naming.NamingContext.lookup(NamingContext.java:781)
org.apache.naming.NamingContext.lookup(NamingContext.java:140)
org.apache.naming.NamingContext.lookup(NamingContext.java:781)
org.apache.naming.NamingContext.lookup(NamingContext.java:153)
org.apache.naming.SelectorContext.lookup(SelectorContext.java:137)
javax.naming.InitialContext.lookup(Unknown Source)
org.apache.jsp._2_jsp._jspService(_2_jsp.java:64)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:121)
上传到空间后,没有办法修改tmcat_home下的server.xml文件,所以在我自己目录下增加META-INF/context.xml文件,和WEB-INF/web.xml来配置。
Context.xml[color=#FF00FF][/color]
<?xml version="1.0" encoding="GBK"?><!--Context path="/" docBase="" debug="5" reloadable="true" crossContext="true"-->
<Context>
<Resource
auth="Container"
description="test"
name="jdbc/connectDB"
type="javax.sql.DataSource"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
maxIdle="2"
maxWait="5000"
username="sa"
password="pwd"
url="jdbc:microsoft:sqlserver://(主机IP):1433;DatabaseName=db"
maxActive="4"/>
</Context>
Web.xml
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd"><web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <description>
Welcome to Tomcat
</description>
<resource-ref>
<description>connectDB</description>
<res-ref-name>jdbc/connectDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth> </resource-ref>
<servlet>
<servlet-name>regservlet</servlet-name>
<display-name>regservlet</display-name>
<description>regservlet</description>
<servlet-class>cn.regservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>regservlet</servlet-name>
<url-pattern>/servlet/regservlet</url-pattern>
</servlet-mapping>
</web-app>测试JSP文件
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.naming.*"%>
<html >
<body >
<%
Context initctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)initctx.lookup("java:comp/env/jdbc/connectDB");
Connection conn = ds.getConnection();
Statement stmt=conn.createStatement();
String sql="select * from AdminInfo";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
out.print("数据库操作成功,恭喜你");
<%}
rs.close();
stmt.close();
conn.close();
%>
</body >
</html >
运行JSP文件,出错。
<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>相对应的jsp或者java代码应该为:DataSource ds = (DataSource) ctx.lookup("java:comp/env/JDBC/TestDB");或者Context envCtx = (Context) ctx.lookup("java:comp/env");
DataSource ds = (DataSource) ctx.lookup("JDBC/TestDB");不配置,或者配置错误,就会出现:Name jdbc is not bound in this Context,如果配置成 <res-ref-name>JDBC/Test222DB</res-ref-name>,则会报Name TestDB is not bound in this Context。注意:<res-ref-name>JDBC/TestDB</res-ref-name> 该配置是大小写敏感的。大小写一定要完全匹配
http://www.knowsky.com/344253.html
在<context></context>中添加
<ResourceLink global="数据源名称" name="映射后的名称" type="javax.sql.DataSource"/>,<ResourceLink global="jdbc/connectDB" name="jdbc/connectDB" type="javax.sql.DataSource"/>,