标签:
1. 应用服务器默认添加的系统属性
NetBeans创建java web project(ctxTest) (index.jsp)
<%@page import="java.util.Properties"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <table> <% Properties props = System.getProperties(); for (String name : props.stringPropertyNames()) { out.println("<tr><td>" + name + ": </td><td>" + props.getProperty(name) + "</td></tr>" ); } %> </table> </body> </html>
访问:http://localhost:7001/ctxTest/index.jsp
2. 文件系统的命名服务 (需要fscontext.jar和providerutil.jar)
NetBeans创建java project(fsContext): (NameServiceTest.java)
package lee; import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class NameServiceTest { public static void main(String[] args) throws NamingException{ final String fileName = "00.目录.docx"; final String dirName = "codes"; Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put(Context.PROVIDER_URL, "file:/D:/JavaEE Traditional/FSContextTest"); Context ctx = new InitialContext(env); Object file = ctx.lookup(fileName); System.out.println(fileName+ " 名称被绑定到: " + file); System.out.println("file的类型是:" + file.getClass()); Object dir = ctx.lookup(dirName); System.out.println(dirName + " 名称被绑定到: " + dir); System.out.println("dir的类型是:" + dir.getClass()); ctx.close(); } }
NameServiceTest2.java
package lee; import javax.naming.*; import java.util.*; public class NameServiceTest2 { public static void main(String[] args) throws NamingException { final String fileName = "youandme.mp3"; final String newName = "油和米.mp3"; final String dirName = "Beyond"; final String newDir1 = "newDir1"; final String newDir2 = "newDir2"; Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put(Context.PROVIDER_URL, "file://D:/JavaEE Traditional/FSContextTest/music"); Context ctx = new InitialContext(env); try { ctx.rename(newName , fileName); } catch (Exception ex) { } try { ctx.destroySubcontext(newDir1); } catch (Exception ex) { } NamingEnumeration<Binding> bindings = ctx.listBindings(""); while(bindings.hasMore()) { Binding binding = bindings.next(); System.out.println(binding.getName() + " --> " + binding.getObject()); } bindings = ctx.listBindings(dirName); System.out.println("------下面是Beyond Context下的绑定------"); while(bindings.hasMore()) { Binding binding = bindings.next(); System.out.println(binding.getName() + " --> " + binding.getObject()); } ctx.createSubcontext(newDir1); ctx.createSubcontext(newDir2); ctx.destroySubcontext(newDir2); ctx.rename(fileName , newName); ctx.close(); } }
2. Weblogic的JNDI支持
JavaEE(2) - Weblogic 服务器执行JNDI绑定和查找
标签:
原文地址:http://www.cnblogs.com/thlzhf/p/4248970.html