Writing Deployment Descriptor of Stateless Session Bean

In this section we will write the deployment descriptor for the session bean.

Writing Deployment Descriptor of Stateless Session Bean

Writing Deployment Descriptor of Stateless Session Bean

      

In this section we will write the deployment descriptor for the session bean. We need the deployment descriptor for application (application.xml), ejb deployment descriptors (ejb-jar.xml and weblogic-ejb-jar.xml) and web.xml files.

Application Deployment descriptor:

We need the following application deployment descriptor to create our ear file (example.ear).

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE application PUBLIC '-//Sun Microsystems,

Inc.//DTD J2EE Application 1.2//EN' 'http://java.sun.com

/j2ee/dtds/application_1_2.dtd'>

<application>
<display-name>Stateless Session Bean Example</dislay-name>
<module>
<web>
<web-uri>example.war</web-uri>
<context-root>/example</context-root>
</web>
</module>

<module>
<ejb>example.jar</ejb>
</module>

</application>

EJB Deployment descriptor:

To create example.jar file we need ejb-jar.xml and weblogic-ejb-jar.xml files. Here is the code for 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 >

<description>EJB Examples</description>
<display-name>EJB Examples</display-name>

<enterprise-beans>

<!-- Session Beans -->
<session >
<description>Shopping Cart Session Bean</description>
<display-name>Shopping Cart Session Bean</display-name>

<ejb-name>ShoppingCart</ejb-name>

<home>examples.ShoppingCartHome</home>
<remote>examples.ShoppingCart</remote>
<ejb-class>examples.ShoppingCartStatefulSessionBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>

</session>

<session >
<description>EJB Test Session Bean</description>
<display-name>EJB Test Session Bean</display-name>

<ejb-name>TestSessionBean</ejb-name>

<home>examples.TestSessionBeanHome</home>
<remote>examples.TestSessionBean</remote>
<local-home>examples.TestSessionBeanLocalHome</local-home>
<local>examples.TestSessionBeanLocal</local>
<ejb-class>examples.MyTestSessionBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>

<env-entry>
<env-entry-name>testSessionBean</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>Test Bean</env-entry-value>
</env-entry>

</session>


<session >
<description>EJB Calculator Session Bean</description>
<display-name>EJB Calculator Session Bean</display-name>

<ejb-name>CalculatorBean</ejb-name>

<home>examples.CalculatorBeanHome</home>
<remote>examples.CalculatorBean</remote>
<local-home>examples.CalculatorBeanLocalHome</local-home>
<local>examples.CalculatorBeanLocal</local>
<ejb-class>examples.CalculatorSessionBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>

<env-entry>
<env-entry-name>CalculatorBean</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>Calculator bean</env-entry-value>
</env-entry>

</session>

</enterprise-beans>

<!-- Relationships -->

<!-- Assembly Descriptor -->
<assembly-descriptor >

<method-permission >
<description>Session Bean</description>
<unchecked/>
<method >
<description>Session Bean</description>
<ejb-name>ShoppingCart</ejb-name>
<method-name>*</method-name>
</method>
</method-permission>

<!-- transactions -->
<container-transaction >
<method >
<ejb-name>ShoppingCart</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Supports</trans-attribute>
</container-transaction>
</assembly-descriptor>

</ejb-jar>

The name of our session bean is TestSessionBean, we will call this bean from servlet (as described in the next page). 

The code for weblogic specific ejb deployment descriptor(weblogic-ejb-jar.xml) is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.

//DTD WebLogic 6.0.0 EJB//EN" "http://www.bea.com/servers

/wls600/dtd/weblogic-ejb-jar.dtd">

<weblogic-ejb-jar>
<description>Session Bean Example</description>
<weblogic-enterprise-bean>
<ejb-name>ShoppingCart</ejb-name>
<stateful-session-descriptor>
</stateful-session-descriptor>
<reference-descriptor>
</reference-descriptor>
<jndi-name>ShoppingCart</jndi-name>
</weblogic-enterprise-bean>
<weblogic-enterprise-bean>
<ejb-name>TestSessionBean</ejb-name>
<stateless-session-descriptor>
</stateless-session-descriptor>
<reference-descriptor>
</reference-descriptor>
<jndi-name>TestSessionBean</jndi-name>
<local-jndi-name>TestSessionBeanLocal</local-jndi-name>
</weblogic-enterprise-bean>
<weblogic-enterprise-bean>
<ejb-name>CalculatorBean</ejb-name>
<stateless-session-descriptor>
</stateless-session-descriptor>
<reference-descriptor>
</reference-descriptor>
<jndi-name>CalculatorSessionBean</jndi-name>
<local-jndi-name>CalculatorBeanLocal</local-jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>

The JNDI name for our Staeless Session bean is TestSessionBean. We will use this JNDI name to lookup the Bean and call it from Servlet.

Web.xml file for war file:

web.xml file describes the example.war file, which is our web application. Here is the code of web.xml file:

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

<servlet>
<servlet-name>sessionTest</servlet-name>
<display-name>Session Test Servlet</display-name>
<servlet-class>net.roseindia.web.servlets.SessionTestServlet</servlet-class>

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

</servlet>

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

</web-app>

Note that in the above web.xml file we have define a servlet named sessionTest. We will use this servlet to call the Staleless Session Bean. Here is the code of servlet used to call session bean:

/*
* SessionTestServlet.java
*
*/

package net.roseindia.web.servlets;

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

import examples.*;


/**
@author Deepak Kumar
* @Web http://www.roseindia.net
* @Email [email protected]
* @web.servlet  name="sessionTest"
*  display-name="Session Test Servlet"
*  load-on-startup="1"
* @web.servlet-mapping url-pattern="/SessionServlet"
*/



public class SessionTestServlet extends HttpServlet {

TestSessionBeanHome testSessionBean;

public void init(ServletConfig configthrows ServletException{
//Look up home interface
try {
 InitialContext ctx = new InitialContext();
 Object objref = ctx.lookup("TestSessionBean");
  testSessionBean = (TestSessionBeanHome)

PortableRemoteObject.narrow(objref,  

TestSessionBeanHome.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("<html>");
out.println("<head>");
out.println("<title>Hello World Servlet!</title>");
out.println("</head>");
out.println("<body>");
out.println("<p align=\"center\"><font size=\"4\" 

color=\"#000080\">Servlet 

Calling Session Bean</font></p>");


try{
TestSessionBean beanRemote;
beanRemote = testSessionBean.create();
out.println("<p align=\"center\"> Message from 

Session Bean is: <b>" 

beanRemote.SayHello() "</b></p>")
beanRemote.remove();
}catch(Exception CreateException){
CreateException.printStackTrace();
}

out.println("<p align=\"center\"><a href=\"javascript

:history.back()\">Go to Home</a></p>");
out.println("</body>");
out.println("</html>");


out.close();
}

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

In the next section I will show you how to deploy and test the application.