• JavaEE(2)


    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();
        }
    }

    3. Weblogic的JNDI支持

    Enviroment-->Servers-->AdminServer(admin)-->View JNDI Tree

    NetBeans创建java web project(bindTest) (index.jsp)

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@ page import="javax.naming.*,javax.swing.*"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>测试JNDI绑定</title>
        </head>
        <body>
            <%
                Context ctx = new InitialContext(); 
                
                final String TEST_JNDI = "testName";
                java.util.Date date = new java.util.Date();
                
                ctx.rebind(TEST_JNDI, date);
                
                out.println("<h3>JNDI绑定成功</h3>");
            %>
        </body>
    </html>

    访问:http://localhost:7001/bindTest/index.jsp

    客户端程序JNDI查找:

    NetBeans创建java project(jndiTest): (JNDIClient.java)

     (需要webservices.jar和wlclient.jar)

    package lee;
    
    import java.util.*;
    import javax.naming.*;
    import javax.swing.*;
    
    public class JNDIClient {
    
        public static void main(String[] args) throws Exception {
            final String INIT_FACTORY = "weblogic.jndi.WLInitialContextFactory";
            final String WL_URL = "t3://localhost:7001";
            
            Hashtable props = new Hashtable();
            props.put(Context.INITIAL_CONTEXT_FACTORY, INIT_FACTORY);
            props.put(Context.PROVIDER_URL, WL_URL);
            
            //    如果需要安全控制,则需要如下两行代码
            //    props.put(Context.SECURITY_PRINCIPAL, "weblogic");
            //    props.put(Context.SECURITY_CREDENTIALS, "weblogic");
            
            //初始化Context,使用InitialContext初始化Context
            Context ctx = new InitialContext(props);
            
            //通过JNDI查找对象,该对象是一个java.util.Date对象。
            Object obj = ctx.lookup("testName");
            System.out.println(obj);
        }
    }

    输出: Sat Jan 24 22:12:32 CST 2015

  • 相关阅读:
    【插件开发】—— 10 JFace开发详解
    百度地图POI数据爬取,突破百度地图API爬取数目“400条“的限制11。
    Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in range(128)
    Python 3.X 要使用urllib.request 来抓取网络资源。转
    python创建目录保存文件
    Python返回数组(List)长度的方法
    python中for、while循环、if嵌套的使用
    (转)python3 urllib.request.urlopen() 错误UnicodeEncodeError: 'ascii' codec can't encode characters
    python 之 string() 模块
    (转)Python3异常-AttributeError: module 'sys' has no attribute 'setdefaultencoding
  • 原文地址:https://www.cnblogs.com/thlzhf/p/4248970.html
Copyright © 2020-2023  润新知