tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marcelo Dias <maru...@gmail.com>
Subject Re: Tomcat+OpenEJB deploying ear files
Date Sun, 17 May 2009 23:28:20 GMT
*David, thank you for your answer.

What I want to do is to use EJB injection into a servlet.

I'll try to describe my project as well as I can. If I missed something let
me know.
I have two servlet classes. One with plain lookup that works, and one with
injection that doesn't work.*

*My Ejb classes are in the package org.maruero.ejbs, here are them:*
*
package org.maruero.ejbs;

import javax.ejb.Stateless;

@Stateless
public class Catalogo implements CatalogoLocal , CatalogoRemote{

    private String[] livros = {"Sharpe","O Imperador"};

    public String[] getLivros(){
        return livros;
    }
}

------------------------------------------------------------------------------------------------------------------------------------------------

package org.maruero.ejbs;

import javax.ejb.Local;

@Local
public interface CatalogoLocal{

    public String[] getLivros();
}

------------------------------------------------------------------------------------------------------------------------------------------------

package org.maruero.ejbs;

import javax.ejb.Remote;

@Remote
public interface CatalogoRemote{

    public String[] getLivros();
}*

*My Servlets classes are in the package org.maruero.client, here are them:*

*package org.maruero.client;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.maruero.ejbs.CatalogoRemote;

public class Client extends HttpServlet{

    @EJB(name="Catalogo")
    private CatalogoRemote catalogo;

    protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
        response.setContentType("text/plain");
        ServletOutputStream out = response.getOutputStream();
        try{
            out.println("Welcome to Client");

            if( catalogo != null )
                for( String livro : catalogo.getLivros() )
                    out.println( livro );
            else
                out.println( catalogo.toString() );

        }catch( Exception e ){
            out.println( e.toString() );
        }
    }
}

--------------------------------------------------------------------------------------------------------------------------------------------

package org.maruero.client;

import java.io.IOException;
import java.util.Properties;

import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.maruero.ejbs.CatalogoRemote;

public class ClientServlet extends HttpServlet{

    protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {

        response.setContentType("text/plain");
        ServletOutputStream out = response.getOutputStream();
        out.println("Welcome to ClientServlet");
        try{
            Properties p = new Properties();
            p.put("java.naming.factory.initial",
"org.apache.openejb.client.RemoteInitialContextFactory");
            p.put("java.naming.provider.url", "
http://127.0.0.1:8080/openejb/ejb");
            InitialContext context = new InitialContext( p );

            Object ref = context.lookup("CatalogoRemote");
            if( ref != null ){
                CatalogoRemote catalogo =
(CatalogoRemote)PortableRemoteObject.narrow(ref,CatalogoRemote.class);
                if( catalogo != null ){
                    for( String livro : catalogo.getLivros() ){
                        out.println(livro);
                    }
                }
            }
        }catch( Exception e ){
            out.println( e.toString() );
        }
    }
}*


*My web.xml is in WebContent/WEB-INF, here is it:*
*
<web-app xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" metadata-complete="false"
version="2.4">
    <display-name>LivrariaClient</display-name>

    <servlet>
        <servlet-class>org.maruero.client.Client</servlet-class>
        <servlet-name>Client</servlet-name>
    </servlet>

    <servlet>
        <servlet-class>org.maruero.client.ClientServlet</servlet-class>
        <servlet-name>ClientServlet</servlet-name>
    </servlet>

    <servlet-mapping>
        <servlet-name>Client</servlet-name>
        <url-pattern>/Client</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>ClientServlet</servlet-name>
        <url-pattern>/ClientServlet</url-pattern>
    </servlet-mapping>


    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>*


*My ejb-jar.xml is in WebContent/META-INF, here is it:*

*<ejb-jar 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/ejb-jar_3_0.xsd"
        version="3.0" metadata-complete="false">

  <enterprise-beans>
      <session>
          <ejb-name>CatalogoRemote</ejb-name>
          <business-remote>org.maruero.ejbs.CatalogoRemote</business-remote>
          <business-local>org.maruero.ejbs.CatalogoLocal</business-local>

          <ejb-class>org.maruero.ejbs.Catalogo</ejb-class>
          <session-type>Stateless</session-type>
          <transaction-type>Container</transaction-type>
      </session>
  </enterprise-beans>

</ejb-jar>*

*My MANIFEST.MF is in WebContent/META-INF, here is it:*

*Manifest-Version: 1.0
Class-Path:*
*
The "ClientServlet" works! It prints in my browser what it's supposed to
print!
It tells me that there isn't something wrong if my EJB classes. Am I right?

But, the Servlet class called "Client" that uses injection doesn't work.

What I'm missing?

Thank you in advance,
Marcelo*

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message