• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Writing myfirst SessionBean

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, Jari. Good job done and very generous of you.
 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There you go!!
I was looking for something like this.
Thanks a lot!!!
Chintan
 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats good for beginner.
Thanx for sharing with us.
Keep it UP !!!

Rashid
reply
    Bookmark Topic Watch Topic
  • New Topic