J2EE下使用JNDI
在J2EE環(huán)境下使用JNDI是非常簡(jiǎn)單的事,因?yàn)樗械腏2EE容器都要實(shí)現(xiàn)JNDI服務(wù),所以,在J2EE環(huán)境下使用JNDI,與使用Hashtable也沒有什么太大區(qū)別。只有一點(diǎn)限制,那就是綁定對(duì)象時(shí),對(duì)象所屬的類必須實(shí)現(xiàn)java.io.Serializable接口,這一點(diǎn)也實(shí)在一點(diǎn)也不困難,幾乎所有用到的Java類都實(shí)現(xiàn)了這個(gè)接口,對(duì)于自定義的類,在接口實(shí)現(xiàn)列表里把這個(gè)接口加進(jìn)去也就是了。
下面,我將演示一下如何在J2EE環(huán)境下使用JNDI,為了保證代碼的通用性,我不使用struts之類的框架,而是直接使用標(biāo)準(zhǔn)JSP和Servlet實(shí)現(xiàn)。我將該項(xiàng)目的名稱定為jndi_test
要使用JNDI,需要先到SUN的網(wǎng)站上去下載jndi.jar。
2.1 JSP
本項(xiàng)目包括5個(gè)JSP,功能說明如下:
index.jsp:首頁(yè)
bind.jsp:用于在JNDI中綁定對(duì)象
bind_result.jsp:綁定對(duì)象后的返回頁(yè)面
lookup.jsp:用于在JNDI中檢索對(duì)象
lookup_result.jsp:用于顯示檢索對(duì)象
本節(jié)中用到的JSP代碼如下,代碼都簡(jiǎn)單地很,就不多做解釋了。
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
本例包括兩個(gè)Servlet,功能說明如下:
BindServlet:用于在JNDI服務(wù)中綁定一個(gè)對(duì)象
LookupServlet:用于在JNDI服務(wù)中取出一個(gè)對(duì)象
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綁定對(duì)象是因?yàn)?,使用bind時(shí),如果已經(jīng)有對(duì)象綁定到該鍵值上,則會(huì)拋出異常。
因?yàn)橹皇鞘纠a,所以我只是綁定了一個(gè)最簡(jiǎn)單的日期對(duì)象。
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下運(yùn)行即可。這樣就可以在J2EE下使用JNDI了。
【編輯推薦】