7 Replies Latest reply on Apr 17, 2003 8:41 AM by yiweya

    Help! A very strange problem!!

    yiweya Newbie

      I have written a session bean named SessionTest.And I write two client programs to test it,one is servlet program and another is JSP page which contains the scriplet to use the session bean.The JSP page works well,but the servlet program encounter a NamingException. However,they contain almost the same code to use the session bean.How strange! Who can tell me why and how to solve this strange problem? Thanks a lot.

      Here is the JSP page:

      <%@ page import="test.bean.*"%>
      <%@ page import="javax.ejb.*"%>
      <%@ page import="javax.naming.*"%>
      <%@ page import="javax.rmi.*"%>
      <%@ page import="java.rmi.*"%>
      <%
      SessionTest sessionTest = null;
      try {
      InitialContext ic = new InitialContext();
      Object objRef = ic.lookup("SessionTest");
      SessionTestHome home = (SessionTestHome) PortableRemoteObject.narrow(
      objRef, SessionTestHome.class);
      sessionTest = home.create();
      out.println ( sessionTest.test() );
      } catch (RemoteException ex) {
      System.out.println("RemoteException : " + ex.getMessage());
      out.println("RemoteException : " + ex.getMessage());
      } catch (CreateException ex) {
      System.out.println("CreateException " + ex.getMessage());
      out.println("CreateException : " + ex.getMessage());
      } catch (NamingException ex) {
      System.out.println("NamingException : " + ex.getMessage());
      out.println("NamingException : " + ex.getMessage());
      }
      %>

      Here is the doPost method in servlet program:

      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.setContentType(CONTENT_TYPE);
      PrintWriter out = response.getWriter();

      SessionTest sessionTest = null;
      try {
      InitialContext ic = new InitialContext();
      Object objRef = ic.lookup("SessionTest");
      SessionTestHome home = (SessionTestHome) PortableRemoteObject.narrow(
      objRef, SessionTestHome.class);
      sessionTest = home.create();
      out.println ( sessionTest.test() );
      } catch (RemoteException ex) {
      System.out.println("RemoteException : " + ex.getMessage());
      out.println("RemoteException : " + ex.getMessage());
      } catch (CreateException ex) {
      System.out.println("CreateException " + ex.getMessage());
      out.println("CreateException : " + ex.getMessage());
      } catch (NamingException ex) {
      System.out.println("NamingException : " + ex.getMessage());
      out.println("NamingException : " + ex.getMessage() + "");
      ex.printStackTrace(out);
      }
      }

      Here is the output of printStackTrace method:

      javax.naming.CommunicationException. Root exception is java.rmi.ConnectException: Connection refused to host: 201.120.113.13; nested exception is:
      java.net.ConnectException: Connection refused: connect
      at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:567)
      at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:185)
      at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:171)
      at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:101)
      at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:492)
      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:471)
      at javax.naming.InitialContext.lookup(InitialContext.java:347)
      at test.ServletTest.doPost(ServletTest.java:36)
      at test.ServletTest.doGet(ServletTest.java:22)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2415)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:509)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:594)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
      at java.lang.Thread.run(Thread.java:536)
      Caused by: java.net.ConnectException: Connection refused: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)
      at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:171)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)
      at java.net.Socket.connect(Socket.java:426)
      at java.net.Socket.connect(Socket.java:376)
      at java.net.Socket.(Socket.java:291)
      at java.net.Socket.(Socket.java:119)
      at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
      at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
      at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:562)
      ... 44 more

      And here is the EJB code:

      //sessionTest.java
      package test.bean;
      import javax.ejb.*;
      import java.rmi.*;

      public interface SessionTest extends javax.ejb.EJBObject {
      public String test () throws RemoteException;
      }

      //sessionTestHome.java
      package test.bean;
      import javax.ejb.*;
      import java.rmi.*;
      import test.*;

      public interface SessionTestHome extends javax.ejb.EJBHome {
      public SessionTest create() throws CreateException, RemoteException;
      }

      //sessionTestBean.java
      package test.bean;
      import javax.ejb.*;

      public class SessionTestBean implements SessionBean {
      //the other code is removed to save space

      public String test () {
      return "In sessionTest bean";
      }
      }

      //ejb-jar.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
      <ejb-jar>
      <enterprise-beans>

      <display-name>SessionTest</display-name>
      <ejb-name>SessionTest</ejb-name>
      test.bean.SessionTestHome
      test.bean.SessionTest
      <ejb-class>test.bean.SessionTestBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>

      </enterprise-beans>
      <assembly-descriptor>
      <container-transaction>

      <ejb-name>SessionTest</ejb-name>
      <method-name>*</method-name>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      </assembly-descriptor>
      </ejb-jar>

      //jboss.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 2.4//EN" "http://www.jboss.org/j2ee/dtd/jboss_2_4.dtd">

      <enterprise-beans>

      <ejb-name>SessionTest</ejb-name>
      <jndi-name>SessionTest</jndi-name>

      </enterprise-beans>