教程集 www.jiaochengji.com
教程集 >  脚本编程  >  java  >  正文 JNDI 笔记(二) J2EE下使用JNDI

JNDI 笔记(二) J2EE下使用JNDI

发布时间:2018-11-15   编辑:jiaochengji.com
教程集为您提供JNDI 笔记(二) J2EE下使用JNDI等资源,欢迎您收藏本站,我们将为您提供最新的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>
<a href="bind.jsp" target="_blank">bind.jsp</a>
<br />
<a 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>
<a 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>
<a 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下运行即可。

您可能感兴趣的文章:
JNDI 笔记(一) 概述
JNDI 笔记(二) J2EE下使用JNDI
JSP中tomcat的SQL Server2000数据库连接池的配置
学习j2ee流程
JBoss配置mysql数据源
Tomcat5配置Mysql JDBC数据库连接池
java 数据库基本操作
1、java数据库操作基本流程
Java数据库编程中的几个常用技巧
数据库连接缓冲池

[关闭]
~ ~