aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » BEA/Weblogic
Bookmark "Writing myfirst SessionBean" Watch "Writing myfirst SessionBean" New topic
Author

Writing myfirst SessionBean

jari bharat
Greenhorn

Joined: Jan 14, 2002
Posts: 12
dear all,
i am new to J2ee technology.in last thread i have post complete step-by-step for accessing access database from servlet running in weblogic.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 java.io.*;
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 {
try{
Hashtable ht = new Hashtable();
PrintWriter out = response.getWriter();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL,
"t3://hostname:7001");

InitialContext ctx = new InitialContext(ht);
Object objref = ctx.lookup("Simple");
home =(SimpleHome)PortableRemoteObject.narrow(
objref,
SimpleHome.class);
out.println(home.create().sayHello());
}
catch (Exception NamingException) {
NamingException.printStackTrace();
}

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

-----------------------------------------------------------------------------------------------------------------

Creating web.xml file
<?xml version="1.0" ?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 1.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>Test</url-pattern>
</servlet-mapping>
</web-app>

---------------------------------------------------------------------------------------------------------
creating ejb-jar.xml file
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN' 'http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd'>
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>Simple</ejb-name>
<home>SimpleHome</home>
<remote>Simple</remote>
<ejb-class>SimpleBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor></assembly-descriptor>
</ejb-jar>
----------------------------------------------------------------------------------------------
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"
"http://www.bea.com/servers/wls600/dtd/weblogic-ejb-jar.dtd" >
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>Simple</ejb-name>
<jndi-name>Simple</jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>

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


ChintanRajyaguru.com
SOADevelopment.com - Coming soon!
Ghulam Rashid
Ranch Hand

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

Rashid
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing myfirst SessionBean