users@glassfish.java.net

Re: glassfish and tomcat when using appserv-rt.jar

From: <glassfish_at_javadesktop.org>
Date: Fri, 04 Jan 2008 12:04:12 PST

Hi, Ron,

I tried a simple webapp running on tomcat 6 that looks up a resource in tomcat, and also accesses an ejb3 deployed on glassfish. I was able to get it work, following these steps:

1 copy glassfish jars to tomcat6/lib
javaee.jar
appserv-rt.jar
appserv-ext.jar
appserv-deployment-client.jar

2 rename appserv-rt.jar to zappserv-rt.jar, to make it to the end of tomcat server classpath. When all jars in tomcat6/lib are loaded, the order is not specified, but it's a common approach to load them alphabetically.

3 restart tomcat

4 My servlet:
public class FooServlet extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        Object name = null;
        Object fooBean = null;
        try {
            InitialContext ic = new InitialContext();
            name = ic.lookup("java:comp/env/name");
            
            Properties props = new Properties();
            props.put("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
            props.put("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
            props.put("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
            props.put(Context.PROVIDER_URL, "iiop://localhost:3700");
            
            InitialContext ic2 = new InitialContext(props);
            fooBean = ic2.lookup("foo.FooRemote");
        } catch (Exception e) {
            throw new ServletException(e);
        }

        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet FooServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("Servlet FooServlet at " + request.getContextPath());
            out.println("name=" + name);
            out.println("fooBean=" + fooBean);
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

5 my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
    <servlet>
        <servlet-name>FooServlet</servlet-name>
        <servlet-class>foo.FooServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>FooServlet</servlet-name>
        <url-pattern>/FooServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    <env-entry>
        <env-entry-name>name</env-entry-name>
        <env-entry-type>java.lang.String</env-entry-type>
        <env-entry-value>catfish</env-entry-value>
    </env-entry>
    </web-app>

6. When running the webpp /catfish/FooServlet, I can see the output showing both lookup are successful:

Servlet FooServlet at /catfish
name=catfish fooBean=foo._FooRemote_Wrapper_at_e9738564

-cheng
[Message sent by forum member 'cf126330' (cf126330)]

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