1 Reply Latest reply on Nov 11, 2005 4:33 PM by Jeremy T

    Can't Access Remote EJB from Servlet

    Jeremy T Newbie

      Environment:
      JDK 1.4.2_09
      JBoss [Zion] 4.0.3SP1 (build: CVSTag=JBoss_4_0_3_SP1 date=200510231054)

      Deployment 1:
      A simple "hello world" Session EJB (ejb 2.1) deployed as an EJB jar.

      Deployment 2:
      A web application (Servlet 2.4) client that has a simple servlet that looks up the remote Session Bean and calls "printHelloWorld". This web app contains the EJB interfaces in a jar file in the /WEB-INF/lib

      I'm getting a ClassCastException when I retrieve the Home interface (see below). Any suggestions on resolving this? I need to test a 2-tier deployment; EJB deployed in one server and the WebClient deployed to another physical tier.

      TIA!

      Specifically:
      6:00:42,732 ERROR [[TestClientServlet]] Servlet.service() for servlet TestClientServlet threw exception
      java.lang.ClassCastException
      at com.sun.corba.se.internal.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:293)
      at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:134)
      at test.TestClientServlet.doGet(TestClientServlet.java:45)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
      at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
      at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:159)
      at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
      at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
      at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
      at java.lang.Thread.run(Thread.java:552)

        • 1. Re: Can't Access Remote EJB from Servlet
          Jeremy T Newbie

          The following are my descriptors:

          For Web Client, snippet:

          <?xml version="1.0" encoding="UTF-8"?>
          
          <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
           <servlet>
           <display-name>TestClientServlet</display-name>
           <servlet-name>TestClientServlet</servlet-name>
           <servlet-class>test.TestClientServlet</servlet-class>
          
           </servlet>
          
          ...
          
           <servlet-mapping>
           <servlet-name>TestClientServlet</servlet-name>
           <url-pattern>/test</url-pattern>
           </servlet-mapping>
          
          ...
           <ejb-ref >
           <ejb-ref-name>ejb/HelloWorldBean</ejb-ref-name>
           <ejb-ref-type>Session</ejb-ref-type>
           <home>test.HelloWorldHome</home>
           <remote>test.HelloWorld</remote>
           </ejb-ref>
          
          </web-app>
          


          For EJB Client, snippet:
          <enterprise-beans>
           <session >
           <description><![CDATA[A session bean named HelloWorld]]></description>
           <display-name>HelloWorld</display-name>
          
           <ejb-name>HelloWorld</ejb-name>
          
           <home>test.HelloWorldHome</home>
           <remote>test.HelloWorld</remote>
           <local-home>test.HelloWorldLocalHome</local-home>
           <local>test.HelloWorldLocal</local>
           <ejb-class>test.HelloWorldSession</ejb-class>
           <session-type>Stateless</session-type>
           <transaction-type>Container</transaction-type>
          
           </session>
          </enterprise-beans>
          


          Servlet Test Code:
          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           try {
           InitialContext ctx = new InitialContext();
           Object ref = ctx.lookup("java:comp/env/ejb/HelloWorldBean");
           HelloWorldHome home = (HelloWorldHome)PortableRemoteObject.narrow(ref, HelloWorldHome.class);
           System.out.println("Test Servlet: received HelloWorldHome: "+home);
           HelloWorld hw = home.create();
           hw.printHelloWorld();
           } catch (Throwable t) {
           throw new ServletException(t);
           }
           }