TOMCAT自定义JNDI

1.简介
tomcat默认提供的jndi配置支持对象有限,比较常用的有datasource,javabean等,
有时无法满足用户的需求 。比如需要在构建对象的构造函数中传递参数等情况。
 
2. 示例
 
使用tomcat的jndi配置url资源
 
myurlfactory.java
===================================================
package com.siyuan.tomcat.jndi;
import java.net.url;<br>import java.util.enumeration;<br>import java.util.hashtable;
import javax.naming.context;<br>import javax.naming.name;<br>import javax.naming.refaddr;<br>import javax.naming.reference;<br>import javax.naming.spi.objectfactory;
public class myurlfactory implements objectfactory {
 public object getobjectinstance(object obj, name name, context namectx,<br>   hashtable environment) throws exception {<br>  <br>  url url = null;<br>  <br>  reference ref = (reference) obj;<br>  enumeration<refaddr> cfgattrs = ref.getall();<br>  while (cfgattrs.hasmoreelements()) {<br>   refaddr cfgattr = cfgattrs.nextelement();<br>   string attrname = cfgattr.gettype();<br>   string attrvalue = (string) cfgattr.getcontent();<br>   if ("url".equals(attrname)) {<br>    url = new url(attrvalue);<br>   }<br>  }<br>  <br>  return url;<br> }
}
 
context.xml
===================================================
<?xml version='1.0' encoding='utf-8'?><br><context><br>     <br>    <resource name="test/url"<br>     auth="container"<br>     type="java.net.url"<br>     factory="com.siyuan.tomcat.jndi.myurlfactory"<br>     url="<a>file:///c:/test.properties"/</a>><br>         <br></context>
 
jndidiv.jsp
===================================================
<jsp:directive.page import="javax.naming.initialcontext"/><br><jsp:directive.page import="javax.naming.context"/><br><jsp:directive.page import="java.net.url"/><br><%<br> context ctxt = new initialcontext();<br> context envctxt = (context) ctxt.lookup("java:comp/env/");<br> url url = (url) envctxt.lookup("test/url");<br>%> <br><%=url %>
 
3.运行结果
 
<img src="http://dl.iteye.com/upload/attachment/484403/68d59461-da03-32af-9aba-31ee0af8a806.jpg" alt="">
 
运行可能出错的原因有:
$tomcat_home$\conf\catalina\localhost\$app_name$.xml 中的内容没有与$app_install_dir$\meta-inf\context.xml同步。
解决方法:
将$app_name$.xml删除后重启app。
 
4.参考资料
 
$tomcat_home$\webapps\docs\jndi-resources-howto.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值