1 Reply Latest reply on Oct 15, 2007 10:49 AM by Peter Johnson

    My first session application

    Nagendra Kumar Newbie

      Hi Friends,

      I am first time using application server, So can any one help me how can i run first session bean in jboss without eclips.

      i have this files;

      package test.session;
      import java.lang.*;
      import java.rmi.RemoteException;
      import javax.ejb.CreateException;
      import javax.ejb.EJBException;
      import javax.ejb.SessionBean;
      import javax.ejb.SessionContext;

      public interface MyTestSession extends javax.ejb.EJBObject{

      public java.lang.String SayHello() throws java.rmi.RemoteException;

      }

      ===============================================================


      package test.session;
      import java.lang.*;
      import java.rmi.RemoteException;
      import javax.ejb.CreateException;
      import javax.ejb.EJBException;
      import javax.ejb.SessionBean;
      import javax.ejb.SessionContext;

      public interface MyTestSessionHome extends javax.ejb.EJBHome{

      public static final String COMP_NAME="java:comp/env/ejb/test/MyTestSession";

      public static final String JNDI_NAME="ejb/test/MyTestSessionBean";

      public test.session.MyTestSession create() throws javax.ejb.CreateException, java.rmi.RemoteException;

      }

      ===========================================

      public class MyTestSessionBean implements SessionBean{

      public void ejbCreate() throws CreateException {

      }

      public String SayHello(){
      String msg="Hello! I am Session Bean";
      System.out.println(msg);
      return msg;
      }



      public void setSessionContext( SessionContext aContext ) throws EJBException {

      }



      public void ejbActivate() throws EJBException {

      }

      public void ejbPassivate() throws EJBException {

      }

      public void ejbRemove() throws EJBException {

      }

      }


      =====================================

      ejb-jar.xml file:

      <?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>

      Example 3
      <display-name>Example 3</display-name>

      <enterprise-beans>

      <!-- Session Beans -->

      <display-name>My Test Session Bean</display-name>
      <ejb-name>test/MyTestSession</ejb-name>
      test.session.MyTestSessionHome
      test.session.MyTestSession
      <ejb-class>test.session.MyTestSessionBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>


      </enterprise-beans>

      <assembly-descriptor>

      </assembly-descriptor>

      </ejb-jar>

      ==============================================

      /*
      * SessionTestServlet.java
      *
      */

      package test.session;

      import javax.servlet.*;
      import javax.servlet.http.*;
      import java.io.*;
      import javax.naming.*;
      import javax.rmi.PortableRemoteObject;


      /**
      * @author Deepak Kumar
      * @Web http://www.roseindia.net
      * @Email deepak@roseindia.net
      */

      public class SessionTestServlet extends HttpServlet {
      MyTestSessionHome testSessionBean;

      public void init(ServletConfig config) throws ServletException{
      //Look up home interface
      try {
      InitialContext ctx = new InitialContext();
      Object objref = ctx.lookup("ejb/test/MyTestSessionBean");
      testSessionBean = (MyTestSessionHome)PortableRemoteObject.narrow(objref, MyTestSessionHome.class);
      } catch (Exception NamingException) {
      NamingException.printStackTrace();
      }


      }

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

      PrintWriter out;
      response.setContentType("text/html");
      String title = "EJB Example";
      out = response.getWriter();

      out.println("");
      out.println("");
      out.println("Hello World Servlet!");
      out.println("");
      out.println("");
      out.println("<p align=\"center\"><font size=\"4\" color=\"#000080\">Servlet Calling Session Bean");


      try{
      MyTestSession beanRemote;
      beanRemote = testSessionBean.create();
      out.println("<p align=\"center\"> Message from Session Bean is: " + beanRemote.SayHello() + "");
      beanRemote.remove();
      }catch(Exception CreateException){
      CreateException.printStackTrace();
      }
      out.println("<p align=\"center\"><a href=\"javascript:history.back()\">Go to Home");
      out.println("");
      out.println("");


      out.close();
      }

      public void destroy() {
      System.out.println("Destroy");
      }
      }

      =======================================
      web.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

      <web-app>

      <servlet-name>SessionServlet</servlet-name>
      <display-name>Simple Session Servlet</display-name>
      <servlet-class>test.session.SessionTestServlet</servlet-class>

      <load-on-startup>1</load-on-startup>



      <servlet-mapping>
      <servlet-name>SessionServlet</servlet-name>
      <url-pattern>/servlet/test</url-pattern>
      </servlet-mapping>

      <session-config>
      <session-timeout>0</session-timeout>
      </session-config>

      </web-app>




      please tell me step by step



      Thanks in adv