Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  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 Calculator Session Bean and Calling through JSP Previous Tutorial Index Next In this lesson I will show you how to develop a Calculator Stateless Session

Tutorial Details:

Bean and call it through JSP file and deploy the web application on JBoss 3.0 Server. This example shows you how to write deployment descriptor for two session beans. In this Lesson we will include the MyTestSession Session Bean developed in Lesson 3. Infact we will use the same development directory structure and add and modify the required file.
So first create our Calculator Session Bean write the deployment descriptor.
Writing Calculator Session Bean
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
public int add(int a, int b)
public int divide(int a, int b) and
public int multiply(int a, int b)
method for calling from JSP. Here is code of our Remote Interface:
package calculator.session;
/**
* @author Deepak Kumar
* @Web http://www.roseindia.net
* @Email deepak@roseindia.net
*/
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 calculatorRemote extends javax.ejb.EJBObject{
public int add(int a, int b) throws RemoteException;
public int divide(int a, int b) throws RemoteException;
public int multiply(int a, int b) throws 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.
package calculator.session;
/**
* @author Deepak Kumar
* @Web http://www.roseindia.net
* @Email deepak@roseindia.net
*/
import javax.ejb.*;
import java.rmi.RemoteException;
import java.lang.*;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public interface calculatorHome extends EJBHome{
public calculatorRemote create() throws RemoteException, CreateException;
}
Enterprise Bean class
All Bean class are defined as public and implements the javax.ejb.SessionBean. In the bean class we have implemented the code for
public int add(int a, int b)
public int divide(int a, int b) and
public int multiply(int a, int b)
Besides this method other required methods which is to be implemented are:
ejbCreate()
ejbRemove()
ejbActivate()
ejbPassivate()
setSessionContext(SessionContext aContext)
Here is the code for our Calculator Session Bean:
/*
* @author Deepak Kumar
* @Web http://www.roseindia.net
* @Email deepak@roseindia.net
*/
package calculator.session;
import javax.ejb.*;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class calculatorBean implements SessionBean{
public void ejbCreate() throws CreateException{
}
public void setSessionContext(SessionContext ctx) throws EJBException{
}
public void ejbRemove() throws EJBException{
}
public void ejbActivate() throws EJBException{
}
public void ejbPassivate() throws EJBException{
}
public int add(int a, int b){
System.out.println("From add function!");
return a+b;
}
public int divide(int a, int b){
System.out.println("From divide function!");
return a/b;
}
public int multiply(int a, int b){
System.out.println("From multiply function!");
return a*b;
}
}
Jar Descriptor File
For creating example.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


Calculator Session Bean
ejb/CalculatorSessionBean
calculator.session.calculatorHome
calculator.session.calculatorRemote
calculator.session.calculatorBean
Stateless
Container





Above deployment descriptor defines remote, home and bean class for the bean and assigns a name ' ejb/CalculatorSessionBean ' to the session bean. Please note that bean of Stateless type and is defined by:
Stateless
jboss.xml file:





test/MyTestSession
ejb/test/MyTestSessionBean


ejb/CalculatorSessionBean
ejb/CalculatorSessionBean





The jboss deployment descriptor assigns jndi name ' ejb/CalculatorSessionBean ' to the ' ejb/CalculatorSessionBean ' bean.
Writing JSP and Web/Ear component
Our JSP file access the session bean and uses it for the calculation and displays the result.
For this purpose we are using add.jsp which displays a form to accept two numbers from the user and submits the form data to calculator.jsp. Here is our add.jsp:
<%@page language="java" %>


Welcome to Jboss 3.0 tutorial




Jboss 3.0 Tutorial

Lesson 4

























Add Numbers


Number 1 :


Number 2 :


 


         



 Back to Lesson 4


 


For more tutorials and examples visit
http://www.roseindia.net


 


Copyright © 2003 roseindia.net. All
rights reserved.




Code for calculator.jsp
<%@page language="java"
import="javax.naming.*"
import="javax.rmi.PortableRemoteObject"
import="calculator.session.*"
import ="test.session.*"
%>
<%
int number1,number2;
number1=Integer.parseInt(request.getParameter("number1"));
number2=Integer.parseInt(request.getParameter("number2"));
calculatorHome calcHome=null;
calculatorRemote calc=null;
InitialContext ic=null;
Object obj;
try{
ic=new InitialContext();
obj=ic.lookup("/ejb/CalculatorSessionBean");
calcHome=(calculatorHome)PortableRemoteObject.narrow(obj,calculatorHome.class);
}catch(Exception e){
System.out.println(e.getMessage());
}
try{
calc=calcHome.create();
}catch(Exception e){
out.println(e.getMessage());
}
%>


Welcome to Jboss 3.0 tutorial



Jboss 3.0 Tutorial

Lesson 4

































Add/Multiply/Divide


Number 1 : = 

<%=number1%>

Number 2 : = 

<%=number2%>

Add (<%=number1%>+<%=number2%>) = 

<%=String.valueOf(calc.add(number1,number2))%>

Multiply (<%=number1%>*<%=number2%>) = 

<%=String.valueOf(calc.multiply(number1,number2))%>

Divide (<%=number1%>/<%=number2%>) = 

<%=String.valueOf(calc.divide(number1,number2))%>

         



On this page we have called Calculator Session Bean to perform Addition, Division and Multiplication.
Our Calculator Session Bean is accessable through JNDI name "/ejb/CalculatorSessionBean".


 Back to Lesson 4


 


For more tutorials and examples visit
http://www.roseindia.net


 


Copyright © 2003 roseindia.net. All
rights reserved.




<%
try{
calc.remove();
}catch(Exception e){
}
//calc=null;
%>
Web-Component Descriptor File
For creating example.war web.xml and jboss-web.xml files are required which explains the content of web archive. We are taking from the previous lesson (Lesson 3).
J2EE Enterprise Archive (ear) Descriptor File
For creating example.ear application.xml , file is required which explains the content of enterprise archive. Please note from this lesson we will creating example.ear for deployment and add access http://localhost:8080/example to access the application.
application.xml file:


Example 4


example.war
/example



example.jar


Above deployment descriptor describes the content of example.ear file which contains to modules one web module example.war and one jar file example.jar .
Writing ant build file and assembling the application into enterprise archive example.ear
I have written ant build for compiling all source files and assembling into enterprise archive eample.ear. Here is the code of ant build file:
build.xml file:




































































Above file does every thing for you and creates example.ear file in the above example4 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/example in the browser. Which displays the following page:
Choose Lesson 4 and following page is displayed:
Click Add/multiply/Divide Function link, this will display the a form to accept two numbers from the user:
Enter the number and press "Submit" button and the calculator.jsp page will display the calculated values for you:
Download the code of all lessons.
In the next lesson I will show you how write Statefull Session Bean.
Previous Tutorial Index Next


 

Rate Tutorial:
http://www.roseindia.net/jboss/calculatorsessionbean.shtml

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
developing a Session Bean and a Servlet and deploy the web application on JBoss 3.0

View Tutorial:
developing a Session Bean and a Servlet and deploy the web application on JBoss 3.0

Related Tutorials:

Secure a Web application, Java-style - JavaWorld April 2000
Secure a Web application, Java-style - JavaWorld April 2000
 
Solid JRun serves up Java on a budget - JavaWorld June 2000
Solid JRun serves up Java on a budget - JavaWorld June 2000
 
Brewing entity Enterprise JavaBeans - JavaWorld September 2000
Brewing entity Enterprise JavaBeans - JavaWorld September 2000
 
Get the app out - JavaWorld January 2001
Get the app out - JavaWorld January 2001
 
Add XML to your J2EE applications - JavaWorld February 2001
Integrate an XML presentation layer in the J2EE layered architecture
 
The art of EJB deployment - JavaWorld August 2001
The art of EJB deployment - JavaWorld August 2001
 
Integrate security infrastructures with JBossSX
Integrate security infrastructures with JBossSX
 
Finalists announced for JavaWorld Editors' Choice Awards
Finalists announced for JavaWorld Editors' Choice Awards
 
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.
 
Add concurrent processing with message-driven beans
Add concurrent processing with message-driven beans
 
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
 
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 Calculator Session Bean and Calling through JSP Previous Tutorial Index Next In this lesson I will show you how to develop a Calculator Stateless Session
 
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
 
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
 
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
 
Creating EJB clients using the Eclipse Rich Client Platform
This article shows how to build a sample EJB client using the Eclipse Rich Client Platform (RCP), which has become increasingly popularity due to its extensible nature.
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.