例如你可以将JNDI配置放在一个叫jndi.properties中,在JSP调用,或者在STRUTS的ACTION中调用:
private static final String JNDIPROPERTIES_FILE = "com.aa.bb.cc";
String jndipropertiesfile = path + getInitParameter("jndi.properties.file");
System.setProperty(JNDIPROPERTIES_FILE,jndipropertiesfile);
在取上下文的时候:
private void loadConfig() throws UOSException {
//导入
Properties properties = new Properties();
FileInputStream fis = null;
try{
String fileName = System.getProperty(JNDIPROPERTIES_FILE);
fis = new FileInputStream(fileName);
properties.load(fis);
_defaultContextFactory = properties.getProperty("java.naming.factory.initial");
_defaultProviderUrl = properties.getProperty("java.naming.provider.url");
_defaultPkgPrefixes = properties.getProperty("java.naming.factory.url.pkgs");
_defaultSecurityPrincipal = properties.getProperty("java.naming.security.principal");
_defaultSecurityCredentials = properties.getProperty("java.naming.security.credentials");
}catch(java.io.FileNotFoundException ex){
throw ex;
}catch(java.io.IOException ex){
throw ex
}finally{
if(fis!=null){
try{
fis.close();
}catch(IOException ex){
}
}
}
}
后调用:
public InitialContext getInitialContext(String contextFactory,
String providerUrl,
String pkgPrefixes,
String username,
String credentials)
throws
NamingException {
StringBuffer ctxName = new StringBuffer();
Properties props = new Properties();
//设置工厂类
if (contextFactory != null) {
props.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,
contextFactory);
ctxName.append(contextFactory);
}
//设置URL链接
if (providerUrl != null) {
props.put(javax.naming.Context.PROVIDER_URL, providerUrl);
ctxName.append(providerUrl);
}
//设置URL连接的包前置名称
if (pkgPrefixes != null) {
props.put(javax.naming.Context.URL_PKG_PREFIXES, pkgPrefixes);
ctxName.append(pkgPrefixes);
}
//设置用户名和密码
if (username != null) {
props.put(Context.SECURITY_PRINCIPAL, username);
ctxName.append(username); if (credentials != null) {
props.put(Context.SECURITY_CREDENTIALS, credentials);
ctxName.append(credentials);
}
}
return new javax.naming.InitialContext(props);
}
private static final String JNDIPROPERTIES_FILE = "com.aa.bb.cc";
String jndipropertiesfile = path + getInitParameter("jndi.properties.file");
System.setProperty(JNDIPROPERTIES_FILE,jndipropertiesfile);
在取上下文的时候:
private void loadConfig() throws UOSException {
//导入
Properties properties = new Properties();
FileInputStream fis = null;
try{
String fileName = System.getProperty(JNDIPROPERTIES_FILE);
fis = new FileInputStream(fileName);
properties.load(fis);
_defaultContextFactory = properties.getProperty("java.naming.factory.initial");
_defaultProviderUrl = properties.getProperty("java.naming.provider.url");
_defaultPkgPrefixes = properties.getProperty("java.naming.factory.url.pkgs");
_defaultSecurityPrincipal = properties.getProperty("java.naming.security.principal");
_defaultSecurityCredentials = properties.getProperty("java.naming.security.credentials");
}catch(java.io.FileNotFoundException ex){
throw ex;
}catch(java.io.IOException ex){
throw ex
}finally{
if(fis!=null){
try{
fis.close();
}catch(IOException ex){
}
}
}
}
后调用:
public InitialContext getInitialContext(String contextFactory,
String providerUrl,
String pkgPrefixes,
String username,
String credentials)
throws
NamingException {
StringBuffer ctxName = new StringBuffer();
Properties props = new Properties();
//设置工厂类
if (contextFactory != null) {
props.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,
contextFactory);
ctxName.append(contextFactory);
}
//设置URL链接
if (providerUrl != null) {
props.put(javax.naming.Context.PROVIDER_URL, providerUrl);
ctxName.append(providerUrl);
}
//设置URL连接的包前置名称
if (pkgPrefixes != null) {
props.put(javax.naming.Context.URL_PKG_PREFIXES, pkgPrefixes);
ctxName.append(pkgPrefixes);
}
//设置用户名和密码
if (username != null) {
props.put(Context.SECURITY_PRINCIPAL, username);
ctxName.append(username); if (credentials != null) {
props.put(Context.SECURITY_CREDENTIALS, credentials);
ctxName.append(credentials);
}
}
return new javax.naming.InitialContext(props);
}
解决方案 »
- 面试题:显式调用构造函数时不能引用实例字段
- dom4j解析xml问题
- struts2访问属性文件失败,在线等。。。。
- SSH关键字查询功能
- 要从读取exel数据生成图表,再写入到pdf中,用什么开发包好
- 空指针错误,谁能帮我解决一下谢谢
- 请问达人们一个处理数字的问题,多谢
- 在hibernate中任何实现这样的sql?
- sql server+weblogic+jbuider怎么使用jdbc?
- 求例子,用JDOM处理XML文档
- 请教高手:struts实例出现错误“Cannot find ActionMappings or ActionFormBeans collection”
- 请问如何设置JBuilder支持JBoss4.x?
让我试试(不过我感觉这不是关键,我感觉好象是布置不正确,在jboss中没看到我的呀)
你这显然是没有部署成功