tomcat中访问JNDI问题

ys333 2005-11-09 09:18:15
在tomcat5中通过在图形界面中建立了JNDI,并且已经相应地修改了客户端的web.xml文件,在程序访问时,tomcat服务器上显示:
Name hnJndi is not bound in this Context
hnxx.Hnxx@b23d12 error DataBase hnJndi Connect Fail,Please wait for 5 minutes!
注:程序是我从weblogic上移值过来的

页面都出现如下情况:

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


root cause

java.lang.NullPointerException
org.apache.jsp.gouwu.login_jsp._jspService(login_jsp.java:73)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


note The full stack trace of the root cause is available in the Tomcat logs.

请大家帮忙看看。
...全文
219 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ys333 2005-11-12
  • 打赏
  • 举报
回复
好的,谢谢了
ys333 2005-11-12
  • 打赏
  • 举报
回复
还有,当我把驱动文件jtds-0.6.jar放到C:\Tomcat 5.0\common\lib时,tomcat启动时就自动关闭了
jackyzgm 2005-11-11
  • 打赏
  • 举报
回复
通过 admin下配置出来的会自动生出来一个,此时server.xml也更改了

其实context.xml内容完全可以写在server.xml内的,

你试试就知道了
叶子哟 2005-11-10
  • 打赏
  • 举报
回复
连接池名称这样写:java:comp/env/jdbc/hnJndi试试
jackyzgm 2005-11-10
  • 打赏
  • 举报
回复
以上是我的亲身配置总结,楼主的问题我都碰到过,一定能很好解决。
jackyzgm 2005-11-10
  • 打赏
  • 举报
回复
关于tomcat 5.5.12 如何配置连接池。

(1)admin登陆后,选 Resources / Data Source,Create New Data Source,创建JNDI后conf/server.xml 里含有以下内容:
(url与driverClassName相对应)
...
<GlobalNamingResources>
<Environment
name="simpleValue"
type="java.lang.Integer"
value="30"/>
<Resource
auth="Container"
description="User database that can be updated and saved"
name="UserDatabase"
type="org.apache.catalina.UserDatabase"
pathname="conf/tomcat-users.xml"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"/>
<Resource
name="jdbc/sqlserver"
type="javax.sql.DataSource"
password="111111"
driverClassName="net.sourceforge.jtds.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="sa"
url="jdbc:jtds:sqlserver://localhost:1433/demo"
maxActive="4"/>
</GlobalNamingResources>
...

(2)conf/context.xml含以下标记

<Context>
...
<ResourceLink name="jdbc/sqlserver" global="jdbc/sqlserver" type="javax.sql.DataSource"/>
</Context>

(3)在webapps下的项目所在的目录的 WEB-INF/web.xml 里含以下标记

<web-app version="2.4">
...
<resource-ref>
<description>MSSQLSERVER Datasource</description>
<res-ref-name>jdbc/sqlserver</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>

(4)编写测试 test.jsp
...
<%@ page import="javax.naming.*" %>
<%@ page import="javax.sql.*" %>
<%
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/sqlserver");
Connection conn = ds.getConnection();
if(conn!=null){
out.print("Connection pool success<br>");
try{
PreparedStatement ps = conn.prepareStatement("select * from table1");
ResultSet rs = ps.executeQuery();
while(rs.next()){
out.print(rs.getString(2)+"<br>");
}
}
catch(Exception e){
out.print("-------- Error: "+e+" -------------<br>");
}
finally{
conn.close(); //释放连接
}
}
%>

(5)重启动tomcat,MSSQLSERVER ,测试通过。
zeq258 2005-11-10
  • 打赏
  • 举报
回复
严重关注!
ys333 2005-11-10
  • 打赏
  • 举报
回复
OperateDb.class:

package hnxx;

import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import sun.jdbc.rowset.CachedRowSet;

public class OperateDb
{

protected String query_statement;
protected String param[];
protected Connection conn;
protected String jndiname;
private String conTime;
private boolean isConnected;

public OperateDb()
{
query_statement = "";
conn = null;
jndiname = "";
isConnected = false;
}

protected void setParam(String param[])
{
this.param = param;
}

private void setQuerystatement(String query_statement)
{
this.query_statement = query_statement;
}

public Connection getConn()
{
return conn;
}

public void dbConn()
throws Exception
{
if(isConnected)
return;
Connection conn1 = null;
DataSource ds = null;
try
{
Context initCtx = new InitialContext();
if(initCtx == null)
throw new Exception(" Context not Find");
if(ds == null)
ds = (DataSource)initCtx.lookup(jndiname);
if(ds != null)
{
conn1 = ds.getConnection();
if(conn1 == null)
throw new Exception(jndiname + " connecte fail! ");
isConnected = true;
conn = conn1;
initCtx.close();
}
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
throw new Exception(toString() + " error DataBase " + jndiname + " Connect Fail,Please Wait For 5 Minutes!");
}
}

public void dbClose()
throws Exception
{
if(conn != null && isConnected && !conn.isClosed())
{
isConnected = false;
conn.close();
}
}

protected ResultSet getResult()
{
ResultSet result = null;
try
{
PreparedStatement select_stm = conn.prepareStatement(query_statement, 1004, 1007);
if(param != null)
{
for(int i = 0; i < param.length; i++)
select_stm.setString(i + 1, param[i]);

}
result = select_stm.executeQuery();
}
catch(Exception e)
{
System.out.println(e);
}
return result;
}

private int updateRecordDb()
throws SQLException, UnsupportedEncodingException
{
dbConn();
PreparedStatement update_stm = conn.prepareStatement(query_statement);
if(param != null)
{
for(int i = 0; i < param.length; i++)
update_stm.setString(i + 1, param[i]);

}
update_stm.executeUpdate();
update_stm.close();
conn.commit();
dbClose();
return 1;
Exception e;
e;
System.out.println(e);
conn.rollback();
return -1;
}

private int updateRecord()
throws SQLException, UnsupportedEncodingException
{
PreparedStatement update_stm = conn.prepareStatement(query_statement);
if(param != null)
{
for(int i = 0; i < param.length; i++)
update_stm.setString(i + 1, param[i]);

}
update_stm.executeUpdate();
update_stm.close();
conn.commit();
return 1;
Exception e;
e;
System.out.println(e);
conn.rollback();
return -1;
}

public boolean isHaveField(String tableName, String colName, String colValue)
{
query_statement = "select count(*) as countnum from " + tableName + " where " + colName + "='" + colValue + "'";
ResultSet result = null;
int rownum;
if(!isConnected)
throw new Exception(jndiname + " not connected");
setQuerystatement(query_statement);
ResultSet result = getResult();
result.next();
rownum = result.getInt(1);
result.close();
if(rownum > 0)
return true;
return false;
Exception e;
e;
System.out.println(e);
return true;
}

public boolean isHaveField(String tableName, String colName, long colValue)
{
query_statement = "select count(*) as countnum from " + tableName + " where " + colName + "=" + colValue;
ResultSet result = null;
int rownum;
dbConn();
if(!isConnected)
throw new Exception(jndiname + " not connected");
setQuerystatement(query_statement);
ResultSet result = getResult();
result.next();
rownum = result.getInt(1);
result.close();
dbClose();
if(rownum > 0)
return true;
return false;
Exception e;
e;
System.out.println(e);
return true;
}

public ResultSet getResult(String sql)
{
ResultSet resault = null;
try
{
if(!isConnected)
throw new Exception(jndiname + " not connected");
setQuerystatement(sql);
resault = getResult();
}
catch(Exception e)
{
System.out.println("getResult: " + sql + ": " + e);
}
return resault;
}

public int exeCute(String sql)
{
if(!isConnected)
throw new Exception(jndiname + " not connected");
setQuerystatement(sql);
return updateRecord();
Exception e;
e;
System.out.println("exeCute: " + sql + ": " + e);
return -1;
}

public int exeSql(String sql)
{
setQuerystatement(sql);
return updateRecordDb();
Exception e;
e;
System.out.println("exeCute: " + sql + ": " + e);
return -1;
}

public CachedRowSet getRowSet(String sql)
{
CachedRowSet rowSet = null;
setQuerystatement(sql);
try
{
dbConn();
if(!isConnected)
throw new Exception(jndiname + " not connected");
PreparedStatement select_stm = conn.prepareStatement(query_statement, 1004, 1008);
if(param != null)
{
for(int i = 0; i < param.length; i++)
select_stm.setString(i + 1, param[i]);

}
ResultSet rs = select_stm.executeQuery();
rowSet = new CachedRowSet();
rowSet.populate(rs);
select_stm.close();
rs.close();
dbClose();
}
catch(SQLException ex)
{
System.out.println("Error occurs in queryRowSet");
rowSet = null;
}
catch(Exception e)
{
System.out.println(e.getMessage());
rowSet = null;
}
finally
{
return rowSet;
}
do
;
while(true);
}

ys333 2005-11-10
  • 打赏
  • 举报
回复
这是其中的三个class文件:
hnxx.class:
package hnxx;
// Referenced classes of package hnxx:
// OperateDb

public class Hnxx extends OperateDb
{

public Hnxx()
{
jndiname = "hnJndi";
}
}

ys333 2005-11-10
  • 打赏
  • 举报
回复
这是server.xml:

<?xml version='1.0' encoding='utf-8'?>
<Server>
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<GlobalNamingResources>
<Environment name="simpleValue" type="java.lang.Integer" value="30"/>
<Resource auth="Container" description="User database that can be updated and saved" name="UserDatabase" type="org.apache.catalina.UserDatabase"/>
<Resource name="jdbc/hnJndi" type="javax.sql.DataSource"/>
<ResourceParams name="UserDatabase">
<parameter>
<name>factory</name>
<value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
</parameter>
<parameter>
<name>pathname</name>
<value>conf/tomcat-users.xml</value>
</parameter>
</ResourceParams>
<ResourceParams name="jdbc/hnJndi">
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>password</name>
<value>ys</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=gouwu</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
</ResourceParams>
</GlobalNamingResources>
<Service name="Catalina">
<Connector acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" port="8080" redirectPort="8443">
</Connector>
<Connector port="8009" protocol="AJP/1.3" protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler" redirectPort="8443">
</Connector>
<Engine defaultHost="localhost" name="Catalina">
<Host appBase="webapps" name="localhost">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_log." suffix=".txt" timestamp="true"/>
</Host>
<Logger className="org.apache.catalina.logger.FileLogger" prefix="catalina_log." suffix=".txt" timestamp="true"/>
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
</Engine>
</Service>
</Server>
ys333 2005-11-10
  • 打赏
  • 举报
回复
我的conf文件夹下怎么没有context.xml文件呀?
shine333 2005-11-09
  • 打赏
  • 举报
回复
tomcat\webapps\tomcat-docs\jndi-resources-howto.html
叶子哟 2005-11-09
  • 打赏
  • 举报
回复
server.xml或是context.xml贴出来

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧