Meaningless Drivel is fun!
The moose likes BEA/Weblogic and the fly likes Writing myfirst SessionBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » BEA/Weblogic
Bookmark "Writing myfirst SessionBean" Watch "Writing myfirst SessionBean" New topic

Writing myfirst SessionBean

jari bharat

Joined: Jan 14, 2002
Posts: 12
dear all,
i am new to J2ee last thread i have post complete step-by-step for accessing access database from servlet running in this this thread i am providing step-by-step for creating Session bean and calling it from Servlet.


Creating Session Bean
1. Creating Home Interface :
import javax.ejb.*;
import java.rmi.*;
public interface SimpleHome extends EJBHome{
public Simple create() throws remoteException, CreateException;

2.create Remote Interface :
import javax.ejb.*;
import java.rmi.*;
public interface Simple extends EJBObject
public String sayHello() throws RemoteException;

Creating Bean
import javax.ejb.*;
public class SimpleBean implements SessionBean

private SessionContext _sessionContext;

// -----------------------------------------------------
// --- Implementation of the Simple interface ----------
// -----------------------------------------------------
public String sayHello()
return "Hello, Enterprise!";
// -----------------------------------------------------
// --- EJB methods -------------------------------------
// -----------------------------------------------------
public void ejbActivate()
public void ejbPassivate()
public void ejbRemove()
public void ejbCreate()
public void setSessionContext(SessionContext sessionContext)
_sessionContext = sessionContext;
Creating Servlet that call Bean
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.util.*;
public class Test extends HttpServlet {
SimpleHome home;
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
Hashtable ht = new Hashtable();
PrintWriter out = response.getWriter();

InitialContext ctx = new InitialContext(ht);
Object objref = ctx.lookup("Simple");
home =(SimpleHome)PortableRemoteObject.narrow(
catch (Exception NamingException) {

public void destroy() {


Creating web.xml file
<?xml version="1.0" ?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 1.2//EN" "">


creating ejb-jar.xml file
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN' ''>
creating weblogic-ejb-jar.xml file
<?xml version="1.0"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC
"-//BEA Systems, Inc.//DTD WebLogic 6.0.0 EJB//EN"
"" >

Deploying Application
Deploying EJB
1.creat temp staging directory.
2.add all ejb class file in root.
3.create META-INF directory as sub-directort of temp.
4. add ejb-jar.xml and weblogic-ejb-jar.xml in it.
5. create jar file by following command.
temp> jar cvf Simple.jar *.*
Deploying Servlet
1.if your are using default web application then add all class file (including ejb classes) in WEB-INF/classes subdirectory located inside default Application folder.
2.add web.xml file in WEB-INF folder.

that's all now u are ready to run your servlet from browser by :
http://hostname ort/Test
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Thanks a lot, Jari. Good job done and very generous of you.
Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
There you go!!
I was looking for something like this.
Thanks a lot!!!
Chintan - Coming soon!
Ghulam Rashid
Ranch Hand

Joined: Jan 14, 2002
Posts: 278
Thats good for beginner.
Thanx for sharing with us.
Keep it UP !!!

I agree. Here's the link:
subject: Writing myfirst SessionBean
It's not a secret anymore!