Tutorial: developing a Session Bean and a Servlet and deploy the web application on
JBoss 3.0
developing a Session Bean and a Servlet and deploy the web application on JBoss 3.0
Writing Stateless Session Bean and Calling through Servlet
Previous Tutorial Index Next
In this lesson I will show you how to develop a Stateless Session Bean and
Tutorial Details:
a Servlet and deploy the web application on JBoss 3.0 Server.
Our application is thin-client multitiered consisting of jsp, servlet and session bean.
In the next lesson we will create Entity bean. So first of all I will explain how to create Session bean and write the deployment descriptor.
Writing Session Bean
Session Bean interacts with the client and is non persistent in nature. If server crashes all the data stored in Session Bean are lost. But Entity Beans are persistent in nature and in case sever crashes Entity Bean reconstruct its data from the underlying database. Session Beans are used to handle the client request and manage the session and Entity Beans are used to do database processing.
Session beans are of two types "Stateful" and "Stateless" . A Stateful session bean preserve the information about its content and values between clients calls. Example of Stateful session bean may be Shopping Cart Session bean. Stateless session bean do not preserve the information between the client calls.
Session Bean is consists of following components:
Enterprise Bean remote interface
Enterprise Bean Home interface
Enterprise bean class definition
Deployment descriptors
Enterprise Bean remote interface
All remote interfaces must extend javax.ejb.EJBObject. Remote interface is the client view of session bean. Methods defined in the remote interface are accessible to the client. In our example we have defined the SayHello() method for calling from servlet. SayHello method is implemented in bean class.
/*
* MyTestSession.java
*
*/
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;
/**
* @author Deepak Kumar
* @Web http://www.roseindia.net
* @Email deepak@roseindia.net
*/
public interface MyTestSession extends javax.ejb.EJBObject{
public java.lang.String SayHello() throws java.rmi.RemoteException;
}
Enterprise Bean Home interface
All home interfaces must extend javax.ejb.EJBHome. ' create()' method of home interface of the application enables the client to create and remove the session object.
/*
* MyTestSessionHome.java
*
*/
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;
/**
* @author Deepak Kumar
* @Web http://www.roseindia.net
* @Email deepak@roseindia.net
*/
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;
}
Enterprise Bean class
All Bean class are defined as public and implements the javax.ejb.SessionBean. In the bean class we have defined SayHello() method, this method is called from our servlet. Besides this method other required methods which is to be implemented are:
ejbCreate()
ejbRemove()
ejbActivate()
ejbPassivate()
setSessionContext(SessionContext aContext)
For the time being these methods are left blank but for advance programming these are used.
/*
* SessionBean.java
*
*/
package test.session;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
/**
* @author Deepak Kumar
* @Web http://www.roseindia.net
* @Email deepak@roseindia.net
*/
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 {
}
}
Jar Descriptor File
For creating example3.jar ejb-jar.xml and jboss.xml files are required which explains the content of jar file.
ejb-jar.xml file:
Example 3 Example 3
My Test Session Bean test/MyTestSession test.session.MyTestSessionHome test.session.MyTestSession test.session.MyTestSessionBean Stateless Container
Above deployment descriptor defines remote, home and bean class for the bean and assigns a name ' test/MyTestSession ' to the session bean. Please note that bean of Stateless type and is defined by:
Stateless jboss.xml file:
test/MyTestSession ejb/test/MyTestSessionBean
The jboss deployment descriptor assigns jndi name ' ejb/test/MyTestSessionBean' to the 'test/MyTestSession' bean.
Writing Servlet class and Web/Ear component
Our servlet access the session bean and calls SayHello() method of the session bean and prints the return string on the browser.
Here is the code of our servlet.
/*
* 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("
");
out.println("");
out.println("");
out.close();
}
public void destroy() {
System.out.println("Destroy");
}
}
Web-Component Descriptor File
For creating example3.war web. xml and jboss-web.xml files are required which explains the content of web archinve.
web.xml file:
Above deployment descriptor defines servlet class for the servlet and assigns url pattern ' /servlet/test ' to the servlet. We call the servlet by tying /servlet/test in the browser.
jboss-web.xml file:
J2EE Enterprise Archive (ear) Descriptor File
For creating example3.ear application.xml , file is required which explains the content of enterprise archive.
application.xml file:
Example 3 example3.war /example3
example3.jar
Above deployment descriptor describes the content of example3.ear file which contains to modules one web module example3.war and one jar file example3.jar .
Writing ant build file and assembling the application into enterprise archive example3.ear
I have written ant build for compiling all source files and assembling into enterprise archive eample3.ear. Here is the code of ant build file:
build .xml file:
Above file does every thing for you and creates example3.ear file in the example3 directory. To assemble the application simple run the ant build utity.
To deploy the application copy the file into the deploy (JBOSS_HOME/server/default/deploy) directory of JBoss 3.0.
To test the application type http://localhost:8080/example3 in the browser and click on the link provided in the index.jsp. Your browser should show the following screen:
Download the code of this lesson.
After completing this lesson you are able to:
Write session bean
Write a servlet
Call session bean from servlet
Write the deployment descriptor files
Write ant build file
Assemble enterprise archive, deploy and test on the JBoss 3 server
Previous Tutorial Index Next
The J2EE 1.4 Tutorial
The J2EE 1.4 Tutorial is a guide to developing enterprise applications for the Java 2 Platform, Enterprise Edition (J2EE) version 1.4. Here we cover all the things you need to know to make the best use of this tutorial.
Building Web Application With Ant and Deploying on Jboss 3.0
This lesson shows you how to build you web application and install on the Jboss 3.0 application server. After the completion of this lesson you will be able to compile, assemble and deploy your J2EE application on Jboss 3.0 application server
Introduction To Enterprise Java Bean(EJB). WebLogic 6.0 Tutorial.
Introduction To Enterprise Java Bean(EJB). WebLogic 6.0 Tutorial.
Welcome to EJB Section
(Learn to Develop World Class Applications with Enterprise Java Beans)
(Online WebLogic 6.0 Tutorial)
Introduction To Enterprise Java Bean(EJB)
Enterprise
Introduction To Enterprise Java Bean(EJB). Developing web component.
Introduction To Enterprise Java Bean(EJB). Developing web component.
Developing web component
Introduction To Java Beans
J2EE specification defines the structure of a J2EE application. According to the specification J2EE application consists of
10 Minutes Guide to Ant
10 Minutes Guide to Ant
10 Minutes Guide to Ant
Previous Tutorial Index Next
Introduction
Well for the next 10 minutes get ready to devote to the ant guide. This will make some sence to the ant.
Ant is a free tool under GNU Licence and is
Building Web Application With Ant and Deploying on Jboss 3.0
Building Web Application With Ant and Deploying on Jboss 3.0
Building Web Application With Ant and Deploying on Jboss 3.0
Previous Tutorial Index Next
In this lesson I will show you how to build you web application and install on the Jboss 3.0
Welcome to the Jboss 3.0 Tutorial
Welcome to the Jboss 3.0 Tutorial
Welcome to the Jboss 3.0 Tutorial
10 Minutes Guide to Ant
Comprehensive description of Ant with example.
Building Web Application With Ant and Deploying on Jboss 3.0
This lesson shows you how to build you web
Struts Guide
Struts Guide
Struts Guide
This tutorial is extensive guide to the Struts Framework. In this tutorial you will learn how to develop robust application using Jakarta Struts Framework. This tutorial assumes that the reader is familiar with the web