1. System properties added by default on the application server
NetBeans Create Java Web Project (ctxtest) (index.jsp)
<%@page Import="java.util.Properties"%><%@page ContentType="text/html"pageencoding="UTF-8"%><!DOCTYPE HTML><HTML> <Head> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"> <title>JSP Page</title> </Head> <Body> <Table> <% Properties props = system.getproperties (); for (StringName:props.stringPropertyNames ()) {Out.println ("<tr><td>" +name+ ": </td><td>" +props.getproperty (name)+ "</td></tr>" ); } %> </Table> </Body></HTML>
Visit:http://localhost:7001/ctxTest/index.jsp
2. File system Naming Service (requires Fscontext.jar and Providerutil.jar)
NetBeans Create Java Project (Fscontext): (Nameservicetest.java)
PackageLee;Importjava.util.Hashtable;ImportJavax.naming.Context;ImportJavax.naming.InitialContext;Importjavax.naming.NamingException; Public classNameservicetest { Public Static voidMain (string[] args)throwsnamingexception{FinalString fileName = "00. Directory. docx"; FinalString dirName = "Codes"; Hashtable Env=NewHashtable (); Env.put (Context.initial_context_factory,"Com.sun.jndi.fscontext.RefFSContextFactory"); Env.put (Context.provider_url,"File:/d:/javaee traditional/fscontexttest"); Context CTX=NewInitialContext (env); Object file = ctx.lookup (fileName); System.out.println (FileName+ "name is bound to:" +file); System.out.println (The type of file is: "+File.getclass ()); Object dir = ctx.lookup (dirName); System.out.println (DirName+ "name is bound to:" +dir); System.out.println (The type of Dir is: "+Dir.getclass ()); Ctx.close (); } }
Nameservicetest2.java
PackageLee;Importjavax.naming.*;ImportJava.util.*; Public classnameservicetest2{ Public Static voidMain (string[] args)throwsnamingexception {FinalString fileName = "Youandme.mp3"; FinalString newName = "Oil and rice. mp3"; FinalString dirName = "Beyond"; FinalString NewDir1 = "NewDir1"; FinalString NewDir2 = "NewDir2"; Hashtable Env=NewHashtable (); Env.put (Context.initial_context_factory,"Com.sun.jndi.fscontext.RefFSContextFactory"); Env.put (Context.provider_url,"File://D:/JavaEE Traditional/fscontexttest/music"); Context CTX=NewInitialContext (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 ("------Below is the binding------under 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. Jndi Support for WebLogic
Java EE (2)-Weblogic server performs JNDI bindings and lookups