users@glassfish.java.net

Re: Remote server: bean call

From: <glassfish_at_javadesktop.org>
Date: Sun, 04 Jul 2010 09:30:42 PDT

Injection (available in JavaEE 5 & 6, or GlassFish 2.x and 3.x) is easier than using traditional lookup. But if you prefer lookup, I've updated my code to get rid of all injections and use lookup only. Note: only need to update servlet3 class, add web.xml and sun-web.xml:

package test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.ejb.EJB;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(loadOnStartup = 1, urlPatterns = { "/Servlet3" })
public class Servlet3 extends HttpServlet {
    // @EJB
    // @EJB(mappedName="corbaname:iiop:1.2_at_gf.sun.com:3700#test.BarIF")

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        FooIF foo;
        BarIF bar;
        foo = lookup(FooIF.class, "java:comp/env/ejb/foo");
        bar = lookup(BarIF.class, "java:comp/env/ejb/bar");
        PrintWriter writer = response.getWriter();
        writer.println("using lookup, foo.hello returned: " + foo.hello(getServletName()));
        writer.println("using lookup, bar.hello returned: " + bar.hello(getServletName()));
    }

    private <T> T lookup(Class<T> cls, String lookupName) throws ServletException {
        T result = null;
        InitialContext ic;
        try {
            ic = new InitialContext();
            result = (T) ic.lookup(lookupName);
        } catch (NamingException e) {
            throw new ServletException(e);
        }
        return result;
    }
}

web.xml:
========
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      version="3.0">
  <ejb-ref>
    <ejb-ref-name>ejb/foo</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
    <remote>test.FooIF</remote>
  </ejb-ref>
  <ejb-ref>
    <ejb-ref-name>ejb/bar</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
    <remote>test.BarIF</remote>
  </ejb-ref>
</web-app>

sun-web.xml (in the same place as web.xml, under WEB-INF)
=========

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Servlet 2.5//EN" "http://www.sun.com/software/sunone/appserver/dtds/sun-web-app_2_5-0.dtd">
<sun-web-app>
    <ejb-ref>
        <ejb-ref-name>ejb/foo</ejb-ref-name>
        <jndi-name>test.FooIF</jndi-name>
    </ejb-ref>
    
    <ejb-ref>
        <ejb-ref-name>ejb/bar</ejb-ref-name>
        <jndi-name>corbaname:iiop:1.2_at_gf.sun.com:3700#test.BarIF</jndi-name>
    </ejb-ref>
    
</sun-web-app>

$ curl http://localhost:8080/web3/Servlet3
using lookup, foo.hello returned: Hello, test.Servlet3 from Mac OS X
using lookup, bar.hello returned: Hello, test.Servlet3 from SunOS

One of the benefits of using a sun-web.xml is, we can remove the machines names from java code, easier to configure.

In general, no-arg constructor of InitialContext should be used, especially inside JavaEE managed environment.

Also for ejb remote invocation (iiop) to work, both ends need to be in the same network.
[Message sent by forum member 'cf126330']

http://forums.java.net/jive/thread.jspa?messageID=476749