日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
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)去也就是了。

創(chuàng)新互聯(lián)主要從事網(wǎng)站制作、成都網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)即墨,10多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792

下面,我將演示一下如何在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:首頁
bind.jsp:用于在JNDI中綁定對(duì)象
bind_result.jsp:綁定對(duì)象后的返回頁面
lookup.jsp:用于在JNDI中檢索對(duì)象
lookup_result.jsp:用于顯示檢索對(duì)象
本節(jié)中用到的JSP代碼如下,代碼都簡(jiǎn)單地很,就不多做解釋了。

2.1.1 index.jsp

 
 
 
  1. < %...@ page language="java" contentType="text/html; charset=GB18030" 
  2.     pageEncoding="GB18030"%> 
  3. < !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  4. < html> 
  5. < head> 
  6. < meta http-equiv="Content-Type" content="text/html; charset=GB18030"> 
  7. < title>JNDI Test< /title> 
  8. < /head> 
  9. < body> 
  10. < a href="bind.jsp" target="_blank">bind.jsp< /a> 
  11. < br /> 
  12. < a href="lookup.jsp" target="_blank">lookup.jsp< /a> 
  13. < /body> 
  14. < /html> 

2.1.2 bind.jsp

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

2.1.3 bind_result.jsp

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

2.1.4 lookup.jsp

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

2.1.5 lookup_result.jsp

 
 
 
  1. < %...@ page language="java" contentType="text/html; charset=GB18030" 
  2.     pageEncoding="GB18030"%> 
  3. < !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  4. < html> 
  5. < head> 
  6. < meta http-equiv="Content-Type" content="text/html; charset=GB18030"> 
  7. < title>JNDI Test - Lookup result< /title> 
  8. < /head> 
  9. < body> 
  10. < %...  
  11.     Object o = request.getAttribute("found_jndi_obj");  
  12.     out.println(o);  
  13. %> 
  14. < /body> 
  15. < /html> 

2.2 Servlet
本例包括兩個(gè)Servlet,功能說明如下:

BindServlet:用于在JNDI服務(wù)中綁定一個(gè)對(duì)象
LookupServlet:用于在JNDI服務(wù)中取出一個(gè)對(duì)象

2.2.1 BindServlet.java

 
 
 
  1. package lld.test.jndi;  
  2.  
  3. import java.io.IOException;  
  4. import java.util.Date;  
  5.  
  6. import javax.naming.Context;  
  7. import javax.naming.InitialContext;  
  8. import javax.servlet.RequestDispatcher;  
  9. import javax.servlet.ServletContext;  
  10. import javax.servlet.ServletException;  
  11. import javax.servlet.http.*;  
  12.  
  13. public class BindServlet extends HttpServlet  
  14. ...{  
  15.  
  16.     private static final long serialVersionUID = 5219969790998794367L;  
  17.  
  18.     @Override 
  19.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  20.             throws ServletException, IOException  
  21.     ...{  
  22.         this.doPost(req, resp);  
  23.     }  
  24.  
  25.     @Override 
  26.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
  27.             throws ServletException, IOException  
  28.     ...{  
  29.         try 
  30.         ...{  
  31.             Context jndi_ctx = new InitialContext();  
  32.             String key = "jndi_object";  
  33.             jndi_ctx.rebind(key, new Date());  
  34.         }catch(Exception ex)  
  35.         ...{  
  36.             ex.printStackTrace();  
  37.         }  
  38.           
  39.         ServletContext context = this.getServletContext();  
  40.         RequestDispatcher dispatcher = context.getRequestDispatcher("/bind_result.jsp");  
  41.         dispatcher.forward(req, resp);  
  42.     }  
  43.       
  44. }  

使用rebind而不是bind綁定對(duì)象是因?yàn)椋褂胋ind時(shí),如果已經(jīng)有對(duì)象綁定到該鍵值上,則會(huì)拋出異常。

因?yàn)橹皇鞘纠a,所以我只是綁定了一個(gè)最簡(jiǎn)單的日期對(duì)象。

2.2.2 LookupServlet.java

 
 
 
  1. package lld.test.jndi;  
  2.  
  3. import java.io.IOException;  
  4.  
  5. import javax.naming.Context;  
  6. import javax.naming.InitialContext;  
  7. import javax.servlet.RequestDispatcher;  
  8. import javax.servlet.ServletContext;  
  9. import javax.servlet.ServletException;  
  10. import javax.servlet.http.HttpServlet;  
  11. import javax.servlet.http.HttpServletRequest;  
  12. import javax.servlet.http.HttpServletResponse;  
  13.  
  14. public class LookupServlet extends HttpServlet  
  15. ...{  
  16.     private static final long serialVersionUID = 6677219828267184673L;  
  17.  
  18.     @Override 
  19.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  20.             throws ServletException, IOException  
  21.     ...{  
  22.         this.doPost(req, resp);  
  23.     }  
  24.  
  25.     @Override 
  26.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
  27.             throws ServletException, IOException  
  28.     ...{  
  29.         try 
  30.         ...{  
  31.             Context jndi_ctx = new InitialContext();  
  32.             String key = "jndi_object";  
  33.             Object o = jndi_ctx.lookup(key);  
  34.             req.setAttribute("found_jndi_obj", o);  
  35.         }catch(Exception ex)  
  36.         ...{  
  37.             ex.printStackTrace();  
  38.         }  
  39.           
  40.         ServletContext context = this.getServletContext();  
  41.         RequestDispatcher dispatcher = context.getRequestDispatcher("/lookup_result.jsp");  
  42.         dispatcher.forward(req, resp);  
  43.     }  
  44.       
  45. }  

2.3 web.xml

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

 
 
 
  1. < ?xml version="1.0" encoding="UTF-8"?> 
  2. < 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"> 
  3.     < display-name>jndi_test< /display-name> 
  4.       
  5.     < servlet> 
  6.         < servlet-name>BindServlet< /servlet-name> 
  7.         < servlet-class>lld.test.jndi.BindServlet< /servlet-class> 
  8.     < /servlet> 
  9.     < servlet-mapping> 
  10.         < servlet-name>BindServlet< /servlet-name> 
  11.         < url-pattern>/bind.do< /url-pattern> 
  12.     < /servlet-mapping> 
  13.       
  14.     < servlet> 
  15.         < servlet-name>LookupServlet< /servlet-name> 
  16.         < servlet-class>lld.test.jndi.LookupServlet< /servlet-class> 
  17.     < /servlet> 
  18.       
  19.     < servlet-mapping> 
  20.         < servlet-name>LookupServlet< /servlet-name> 
  21.         < url-pattern>/lookup.do< /url-pattern> 
  22.     < /servlet-mapping> 
  23.       
  24.     < welcome-file-list> 
  25.         < welcome-file>index.jsp< /welcome-file> 
  26.     < /welcome-file-list> 
  27. < /web-app> 

OK,所有的代碼都在這里了,部署到Tomcat下運(yùn)行即可。這樣就可以在J2EE下使用JNDI了。


文章標(biāo)題:J2EE下使用JNDI
標(biāo)題URL:http://www.dlmjj.cn/article/cdihjdg.html