protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
try {
Context initialContext = new InitialContext();
String a = "zyy: aaaaaaaaaaaaaa10";
initialContext.bind("some/where/over/there", a);
LinkRef linkref = new LinkRef("some/where");
initialContext.bind("here", linkref);
PrintWriter out = response.getWriter();
Thread.currentThread().setContextClassLoader(oldCL);
out.println("test lookupLink");
try {
LinkRef value1 = (LinkRef) initialContext.lookupLink("here");
out.println("here value 111111111:  " + value1);
if (value1 != null)
out.println("here value 2222222222222:  " + value1.getLinkName());
LinkRef value = (LinkRef) initialContext
.lookupLink("here/over/there");
out.println("here value 3333333333333:  " + value);
if (value != null)
out.println("zyy value:  " + value.getLinkName());
} catch (NamingException e) {
//out.println("zyy not found!");
e.printStackTrace();
}
} catch (NamingException e) {
e.printStackTrace();
} finally {
Context initialContext;
try {
initialContext = new InitialContext();
initialContext.unbind("some/where/over/there");
initialContext.unbind("here");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以上Servlet输出结果是什么?在所有jee应用服务器上都出错.那么lookupLink到底如何用,LinkRef到底如何用?谁能给个例子?

解决方案 »

  1.   

    说说什么错误,一般 java: 这个协议(也就是 J2EE 应用服务器容器内工作时)都是自动把 lookup("java:xxxx") 的 Link 解析成原始 url 对应的对象。这是 应用服务器提供的实现的一般做法,可能是强制标准来支持 J2EE 应用程序的资源引用的自动完成呢,因为不自动解析,那我们程序的资源引用需要 lookup 多次呢。而 LDAP 之类的协议都不会自动解析。
      

  2.   

    weblogic : 
    在 LinkRef value = (LinkRef) initialContext.lookupLink("here/over/there");出错
    报over.there 无法解析Tomcat 直接是就是name not found 异常
      

  3.   

    有兴趣的哥们把这个Servlet完善在不同应用服务器跑跑就知道结果
      

  4.   

    我略改了点,让它在 JBoss 上跑起来了。使用 lookup("here/over/there") 和 lookupLink("here/over/there") 都能工作。我想你已经知道了 LinkRef 有什么用,但只想试一下,因为 here/over/there 不是一个 LinkRef 而是 绑定对象, 所以 lookupLink("here/over/there") 就不合理,但不出错,要知道返回的对象不是期望的结果。J2EE 中我们直接 lookup 就可以了,lookupLink 也就只需要让容器使用就够了,因为我们程序只负责 lookup 并不负责 bind,而我们也不必须知道这个 LinkRef 本来指向了什么地方,因为那是 J2EE 应用服务器来负责的范围,为了保持移植性应用程序不应该知道这个细节。使用 lookup("here/over/there") 时:
    13:49:22,796 INFO  [STDOUT] test lookupLink
    13:49:22,796 INFO  [STDOUT] here value 111111111: Reference Class Name: javax.naming.LinkRef
    Type: LinkAddress
    Content: some/where
    13:49:22,796 INFO  [STDOUT] here value 2222222222222: some/where
    13:49:22,796 INFO  [STDOUT] here value 3333333333333: zyy: aaaaaaaaaaaaaa10
    13:49:22,796 INFO  [STDOUT] zyy value: zyy: aaaaaaaaaaaaaa10使用 lookupLink("here/over/there") 时:
    13:49:22,796 INFO  [STDOUT] test lookupLink
    13:49:22,796 INFO  [STDOUT] here value 111111111: Reference Class Name: javax.naming.LinkRef
    Type: LinkAddress
    Content: some/where
    13:49:22,796 INFO  [STDOUT] here value 2222222222222: some/where
    13:49:22,796 INFO  [STDOUT] here value 3333333333333: javax.naming.spi.ResolveResult@1cacbd9
    13:49:22,796 INFO  [STDOUT] zyy value: javax.naming.spi.ResolveResult@1cacbd9
    try {
    Context initialContext = new InitialContext();
    String a = "zyy: aaaaaaaaaaaaaa10";
    Context ctx = initialContext; try {
    ctx = (Context) initialContext.lookup("some");
    } catch (NameNotFoundException e) {
    ctx = initialContext.createSubcontext("some");
    } try {
    ctx = (Context) initialContext.lookup("where");
    } catch (NameNotFoundException e) {
    ctx = initialContext.createSubcontext("where");
    } try {
    ctx = (Context) initialContext.lookup("over");
    } catch (NameNotFoundException e) {
    ctx = initialContext.createSubcontext("over");
    } initialContext.bind("some/where/over/there", a);
    LinkRef linkref = new LinkRef("some/where");
    initialContext.bind("here", linkref); System.out.println("test lookupLink");
    try {
    LinkRef value1 = (LinkRef) initialContext.lookupLink("here");
    System.out.println("here value 111111111: " + value1);
    if (value1 != null)
    System.out.println("here value 2222222222222: " + value1.getLinkName());
    Object value = initialContext.lookup("here/over/there"); 或 lookupLink 都可工作,但 lookupLink 返回 javax.naming.spi.ResolveResult,返回的结果类型不同,原来的对象已经解析了。
    System.out.println("here value 3333333333333: " + value);
    if (value != null)
    System.out.println("zyy value: " + value);
    } catch (NamingException e) {
    // out.println("zyy not found!");
    e.printStackTrace();
    }
    } catch (NamingException e) {
    e.printStackTrace();
    } finally {
    Context initialContext;
    try {
    initialContext = new InitialContext();
    initialContext.unbind("some/where/over/there");
    initialContext.unbind("here");
    } catch (NamingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
      

  5.   

    老兄,你是在JBoss 什么版本上跑的,你这个程序那我在JBoss 5上跑怎么不行呢13:51:45,203 ERROR [STDERR] javax.naming.NameNotFoundException: where not bound
    13:51:45,203 ERROR [STDERR]     at org.jnp.server.NamingServer.getBinding(Naming
    Server.java:771)
    13:51:45,218 ERROR [STDERR]     at org.jnp.server.NamingServer.getBinding(Naming
    Server.java:779)
    13:51:45,218 ERROR [STDERR]     at org.jnp.server.NamingServer.getObject(NamingS
    erver.java:785)
    13:51:45,265 ERROR [STDERR]     at org.jnp.server.NamingServer.unbind(NamingServ
    er.java:315)
    13:51:45,265 ERROR [STDERR]     at org.jnp.server.NamingServer.unbind(NamingServ
    er.java:318)
    13:51:45,265 ERROR [STDERR]     at org.jnp.interfaces.NamingContext.unbind(Namin
    gContext.java:871)
    13:51:45,265 ERROR [STDERR]     at org.jnp.interfaces.NamingContext.unbind(Namin
    gContext.java:854)
    13:51:45,265 ERROR [STDERR]     at javax.naming.InitialContext.unbind(InitialCon
    text.java:416)
    13:51:45,265 ERROR [STDERR]     at org.apache.jsp.TestJndi_jsp._jspService(TestJ
    ndi_jsp.java:112)
    13:51:45,265 ERROR [STDERR]     at org.apache.jasper.runtime.HttpJspBase.service
    (HttpJspBase.java:70)
    13:51:45,265 ERROR [STDERR]     at javax.servlet.http.HttpServlet.service(HttpSe
    rvlet.java:717)
    13:51:45,265 ERROR [STDERR]     at org.apache.jasper.servlet.JspServletWrapper.s
    ervice(JspServletWrapper.java:369)
    13:51:45,265 ERROR [STDERR]     at org.apache.jasper.servlet.JspServlet.serviceJ
    spFile(JspServlet.java:322)
    13:51:45,265 ERROR [STDERR]     at org.apache.jasper.servlet.JspServlet.service(
    JspServlet.java:249)
    13:51:45,265 ERROR [STDERR]     at javax.servlet.http.HttpServlet.service(HttpSe
    rvlet.java:717)
    13:51:45,265 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterCha
    in.internalDoFilter(ApplicationFilterChain.java:290)
    13:51:45,265 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterCha
    in.doFilter(ApplicationFilterChain.java:206)
    13:51:45,265 ERROR [STDERR]     at org.jboss.web.tomcat.filters.ReplyHeaderFilte
    r.doFilter(ReplyHeaderFilter.java:96)
    13:51:45,265 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterCha
    in.internalDoFilter(ApplicationFilterChain.java:235)
    13:51:45,265 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterCha
    in.doFilter(ApplicationFilterChain.java:206)
    13:51:45,265 ERROR [STDERR]     at org.apache.catalina.core.StandardWrapperValve
    .invoke(StandardWrapperValve.java:235)
    13:51:45,265 ERROR [STDERR]     at org.apache.catalina.core.StandardContextValve
    .invoke(StandardContextValve.java:191)
    13:51:45,265 ERROR [STDERR]     at org.jboss.web.tomcat.security.SecurityAssocia
    tionValve.invoke(SecurityAssociationValve.java:190)
    13:51:45,265 ERROR [STDERR]     at org.jboss.web.tomcat.security.JaccContextValv
    e.invoke(JaccContextValve.java:92)
    13:51:45,265 ERROR [STDERR]     at org.jboss.web.tomcat.security.SecurityContext
    EstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
    13:51:45,265 ERROR [STDERR]     at org.jboss.web.tomcat.security.SecurityContext
    EstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
    13:51:45,265 ERROR [STDERR]     at org.apache.catalina.core.StandardHostValve.in
    voke(StandardHostValve.java:127)
    13:51:45,265 ERROR [STDERR]     at org.apache.catalina.valves.ErrorReportValve.i
    nvoke(ErrorReportValve.java:102)
    13:51:45,265 ERROR [STDERR]     at org.jboss.web.tomcat.service.jca.CachedConnec
    tionValve.invoke(CachedConnectionValve.java:158)
    13:51:45,265 ERROR [STDERR]     at org.apache.catalina.core.StandardEngineValve.
    invoke(StandardEngineValve.java:109)
    13:51:45,265 ERROR [STDERR]     at org.apache.catalina.connector.CoyoteAdapter.s
    ervice(CoyoteAdapter.java:330)
    13:51:45,265 ERROR [STDERR]     at org.apache.coyote.http11.Http11Processor.proc
    ess(Http11Processor.java:829)
    13:51:45,265 ERROR [STDERR]     at org.apache.coyote.http11.Http11Protocol$Http1
    1ConnectionHandler.process(Http11Protocol.java:598)
    13:51:45,281 ERROR [STDERR]     at org.apache.tomcat.util.net.JIoEndpoint$Worker
    .run(JIoEndpoint.java:447)
    13:51:45,281 ERROR [STDERR]     at java.lang.Thread.run(Thread.java:662)
    13:51:48,187 ERROR [STDERR] javax.naming.NameNotFoundException: where not bound
    13:51:48,187 ERROR [STDERR]     at org.jnp.server.NamingServer.getBinding(Naming
    Server.java:771)
    13:51:48,187 ERROR [STDERR]     at org.jnp.server.NamingServer.getBinding(Naming
    Server.java:779)
    13:51:48,187 ERROR [STDERR]     at org.jnp.server.NamingServer.getObject(NamingS
    erver.java:785)
    13:51:48,187 ERROR [STDERR]     at org.jnp.server.NamingServer.bind(NamingServer
    .java:161)
    13:51:48,187 ERROR [STDERR]     at org.jnp.server.NamingServer.bind(NamingServer
    .java:167)
    13:51:48,203 ERROR [STDERR]     at org.jnp.interfaces.NamingContext.bind(NamingC
    ontext.java:650)
    13:51:48,203 ERROR [STDERR]     at org.jnp.interfaces.NamingContext.bind(NamingC
    ontext.java:611)
    13:51:48,203 ERROR [STDERR]     at javax.naming.InitialContext.bind(InitialConte
    xt.java:400)
    13:51:48,203 ERROR [STDERR]     at org.apache.jsp.TestJndi_jsp._jspService(TestJ
    ndi_jsp.java:88)
    13:51:48,203 ERROR [STDERR]     at org.apache.jasper.runtime.HttpJspBase.service
    (HttpJspBase.java:70)
    13:51:48,203 ERROR [STDERR]     at javax.servlet.http.HttpServlet.service(HttpSe
    rvlet.java:717)
    13:51:48,203 ERROR [STDERR]     at org.apache.jasper.servlet.JspServletWrapper.s
    ervice(JspServletWrapper.java:369)
    13:51:48,203 ERROR [STDERR]     at org.apache.jasper.servlet.JspServlet.serviceJ
    spFile(JspServlet.java:322)
    13:51:48,203 ERROR [STDERR]     at org.apache.jasper.servlet.JspServlet.service(
    JspServlet.java:249)
    13:51:48,203 ERROR [STDERR]     at javax.servlet.http.HttpServlet.service(HttpSe
    rvlet.java:717)
    13:51:48,203 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterCha
    in.internalDoFilter(ApplicationFilterChain.java:290)
    13:51:48,203 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterCha
    in.doFilter(ApplicationFilterChain.java:206)
    13:51:48,203 ERROR [STDERR]     at org.jboss.web.tomcat.filters.ReplyHeaderFilte
    r.doFilter(ReplyHeaderFilter.java:96)
    13:51:48,203 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterCha
    in.internalDoFilter(ApplicationFilterChain.java:235)
    13:51:48,203 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterCha
    in.doFilter(ApplicationFilterChain.java:206)
    13:51:48,203 ERROR [STDERR]     at org.apache.catalina.core.StandardWrapperValve
    .invoke(StandardWrapperValve.java:235)
    13:51:48,203 ERROR [STDERR]     at org.apache.catalina.core.StandardContextValve
    .invoke(StandardContextValve.java:191)
    13:51:48,203 ERROR [STDERR]     at org.jboss.web.tomcat.security.SecurityAssocia
    tionValve.invoke(SecurityAssociationValve.java:190)
    13:51:48,203 ERROR [STDERR]     at org.jboss.web.tomcat.security.JaccContextValv
    e.invoke(JaccContextValve.java:92)
    13:51:48,203 ERROR [STDERR]     at org.jboss.web.tomcat.security.SecurityContext
    EstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
    13:51:48,203 ERROR [STDERR]     at org.jboss.web.tomcat.security.SecurityContext
    EstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
    13:51:48,203 ERROR [STDERR]     at org.apache.catalina.core.StandardHostValve.in
    voke(StandardHostValve.java:127)
    13:51:48,203 ERROR [STDERR]     at org.apache.catalina.valves.ErrorReportValve.i
    nvoke(ErrorReportValve.java:102)
    13:51:48,203 ERROR [STDERR]     at org.jboss.web.tomcat.service.jca.CachedConnec
    tionValve.invoke(CachedConnectionValve.java:158)
    13:51:48,203 ERROR [STDERR]     at org.apache.catalina.core.StandardEngineValve.
    invoke(StandardEngineValve.java:109)
    13:51:48,203 ERROR [STDERR]     at org.apache.catalina.connector.CoyoteAdapter.s
    ervice(CoyoteAdapter.java:330)
    13:51:48,203 ERROR [STDERR]     at org.apache.coyote.http11.Http11Processor.proc
    ess(Http11Processor.java:829)
    13:51:48,234 ERROR [STDERR]     at org.apache.coyote.http11.Http11Protocol$Http1
    1ConnectionHandler.process(Http11Protocol.java:598)
    13:51:48,234 ERROR [STDERR]     at org.apache.tomcat.util.net.JIoEndpoint$Worker
    .run(JIoEndpoint.java:447)
    13:51:48,234 ERROR [STDERR]     at java.lang.Thread.run(Thread.java:662)
    13:51:48,234 ERROR [STDERR] javax.naming.NameNotFoundException: where not bound
    13:51:48,234 ERROR [STDERR]     at org.jnp.server.NamingServer.getBinding(Naming
    Server.java:771)
    13:51:48,234 ERROR [STDERR]     at org.jnp.server.NamingServer.getBinding(Naming
    Server.java:779)
    13:51:48,234 ERROR [STDERR]     at org.jnp.server.NamingServer.getObject(NamingS
    erver.java:785)
    13:51:48,234 ERROR [STDERR]     at org.jnp.server.NamingServer.unbind(NamingServ
    er.java:315)