JNDI 笔记(二) J2EE下使用JNDI


在J2EE环境下使用JNDI是非常简单的事,因为所有的J2EE容器都要实现JNDI服务,所以,在J2EE环境下使用JNDI,与使用Hashtable也没有什么太大区别。只有一点限制,那就是绑定对象时,对象所属的类必须实现java.io.Serializable接口,这一点也实在一点也不困难,几乎所有用到的Java类都实现了这个接口,对于自定义的类,在接口实现列表里把这个接口加进去也就是了。

下面,我将演示一下如何在J2EE环境下使用JNDI,为了保证代码的通用性,我不使用struts之类的框架,而是直接使用标准JSP和Servlet实现。我将该项目的名称定为jndi_test

要使用JNDI,需要先到SUN的网站上去下载jndi.jar。

 2.1 JSP

本项目包括5个JSP,功能说明如下:

  • index.jsp:首页
  • bind.jsp:用于在JNDI中绑定对象
  • bind_result.jsp:绑定对象后的返回页面
  • lookup.jsp:用于在JNDI中检索对象
  • lookup_result.jsp:用于显示检索对象

本节中用到的JSP代码如下,代码都简单地很,就不多做解释了。

2.1.1 index.jsp

<% @ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding
="GB18030"
%>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=GB18030" >
< title > JNDI Test </ title >
</ head >
< body >
< href ="bind.jsp"  target ="_blank" > bind.jsp </ a >
< br  />
< href ="lookup.jsp"  target ="_blank" > lookup.jsp </ a >
</ body >
</ html >

2.1.2 bind.jsp

<% @ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding
="GB18030"
%>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=GB18030" >
< title > JNDI Test - Bind </ title >
</ head >
< body >
< href ="bind.do" > bind an object </ a >
</ body >
</ html >

2.1.3 bind_result.jsp

<% @ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding
="GB18030"
%>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=GB18030" >
< title > JNDI Test - Bind result </ title >
</ head >
< body >
< p > Binded successfully! </ p >
</ body >
</ html >

2.1.4 lookup.jsp

<% @ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding
="GB18030"
%>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=GB18030" >
< title > JNDI Test - lookup </ title >
</ head >
< body >
< href ="lookup.do" > lookup the binded object </ a >
</ body >
</ html >

2.1.5 lookup_result.jsp

<% @ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding
="GB18030"
%>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=GB18030" >
< title > JNDI Test - Lookup result </ title >
</ head >
< body >
<%
    
Object o = request.getAttribute("found_jndi_obj");
    out.println(o);
%>
</ body >
</ html >

2.2 Servlet

本例包括两个Servlet,功能说明如下:

  • BindServlet:用于在JNDI服务中绑定一个对象
  • LookupServlet:用于在JNDI服务中取出一个对象

2.2.1 BindServlet.java

package  lld.test.jndi;

import  java.io.IOException;
import  java.util.Date;

import  javax.naming.Context;
import  javax.naming.InitialContext;
import  javax.servlet.RequestDispatcher;
import  javax.servlet.ServletContext;
import  javax.servlet.ServletException;
import  javax.servlet.http. * ;

public   class  BindServlet  extends  HttpServlet
{

    
private static final long serialVersionUID = 5219969790998794367L;

    @Override
    
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            
throws ServletException, IOException
    
{
        
this.doPost(req, resp);
    }


    @Override
    
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            
throws ServletException, IOException
    
{
        
try
        
{
            Context jndi_ctx 
= new InitialContext();
            String key 
= "jndi_object";
            jndi_ctx.rebind(key, 
new Date());
        }
catch(Exception ex)
        
{
            ex.printStackTrace();
        }

        
        ServletContext context 
= this.getServletContext();
        RequestDispatcher dispatcher 
= context.getRequestDispatcher("/bind_result.jsp");
        dispatcher.forward(req, resp);
    }

    
}

使用rebind而不是bind绑定对象是因为,使用bind时,如果已经有对象绑定到该键值上,则会抛出异常。

因为只是示例代码,所以我只是绑定了一个最简单的日期对象。

2.2.2 LookupServlet.java

package  lld.test.jndi;

import  java.io.IOException;

import  javax.naming.Context;
import  javax.naming.InitialContext;
import  javax.servlet.RequestDispatcher;
import  javax.servlet.ServletContext;
import  javax.servlet.ServletException;
import  javax.servlet.http.HttpServlet;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;

public   class  LookupServlet  extends  HttpServlet
{
    
private static final long serialVersionUID = 6677219828267184673L;

    @Override
    
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            
throws ServletException, IOException
    
{
        
this.doPost(req, resp);
    }


    @Override
    
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            
throws ServletException, IOException
    
{
        
try
        
{
            Context jndi_ctx 
= new InitialContext();
            String key 
= "jndi_object";
            Object o 
= jndi_ctx.lookup(key);
            req.setAttribute(
"found_jndi_obj", o);
        }
catch(Exception ex)
        
{
            ex.printStackTrace();
        }

        
        ServletContext context 
= this.getServletContext();
        RequestDispatcher dispatcher 
= context.getRequestDispatcher("/lookup_result.jsp");
        dispatcher.forward(req, resp);
    }

    
}

2.3 web.xml

在web.xml中,加入了servlet映射

<? xml version="1.0" encoding="UTF-8" ?>
< web-app  id ="WebApp_ID"  version ="2.4"  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" >
    
< display-name > jndi_test </ display-name >
    
    
< servlet >
        
< servlet-name > BindServlet </ servlet-name >
        
< servlet-class > lld.test.jndi.BindServlet </ servlet-class >
    
</ servlet >
    
< servlet-mapping >
        
< servlet-name > BindServlet </ servlet-name >
        
< url-pattern > /bind.do </ url-pattern >
    
</ servlet-mapping >
    
    
< servlet >
        
< servlet-name > LookupServlet </ servlet-name >
        
< servlet-class > lld.test.jndi.LookupServlet </ servlet-class >
    
</ servlet >
    
    
< servlet-mapping >
        
< servlet-name > LookupServlet </ servlet-name >
        
< url-pattern > /lookup.do </ url-pattern >
    
</ servlet-mapping >
    
    
< welcome-file-list >
        
< welcome-file > index.jsp </ welcome-file >
    
</ welcome-file-list >
</ web-app >

OK,所有的代码都在这里了,部署到Tomcat下运行即可。

2.4 示例下载

嗯, 我不晓得如何上传文件, 谁想要发我邮箱

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值