Statefull Session Bean on Oracle WebLogic Server 10.3.2

Let us develop and deploy Stateful Session Bean on Oracle Weblogic Server 10.3.2

The implementation of the callback methods is different depending on whether it is a stateful or stateless.
[ Note : No need Deployment descriptor EJB Configuration XML file ]

Below steps are to create Stateful Session Bean :

  • Install Oracle WebLogic Server 10.3.2
  • Create Domain on Orcale WebLogic Server 10.3.2
  • Configure Domain in Eclipse
  • Create Enterprise Application StatefulDemo1.ear
  • Create user defined interface
  • Create Stateful Session Bean with Remote and Local in EJB project
  • Add EJB Project in Web Project
  • Create EJBLookup.java to find stateful component
  • Create index.jsp in Web project
  • Create Servlet in Web project
  • Run the Application

Step 1: Install Oracle WebLogic Server 10.3.2 [ Click here to know how to install WebLogic Server ]

Step 2: Create Domain on Orcale WebLogic Server 10.3.2 [ Click here to know how to create Domain on WebLogic Server ]

Step 3: Configure Domain in Eclipse [ Click here to know how to Configure Domain in Eclipse ]

Step 4: Create Enterprise Application StatefulDemo1
File -> New -> Enterprise Appliction Project
Stateful Session Bean
Specify the Enterprise Project name StatefulDemo1 and click Next.

Below screen is used to create EJB and Web module in Enterprise project. Click New Module
Stateful Session Bean
Check EJB Module and Web Module and Click Finish.
This step will create Enterprise project with EJB and Web module.

Step 5: Create user defined interface
StatefullInterface.java
package techmyguru.stateful;
import java.util.ArrayList;
public interface StatefullInterface 
{
public void setUser(String user);
public String getUser();
public String addItem(String item);
public String deleteItem(String item);
public ArrayList<String> getItems();
public void destroy();
}

Step 6: Create Stateful Session Bean in EJB project
Right click on EJB project -> New -> Session Bean (EJB3.X)
Stateful Session Bean
This step will create three java files. One local interface StatefullSession1Local.java, remote interface StatefullSession1Remote.java and bean class StatefullSession1.java.

StatefullSession1Local.java
package techmyguru.stateful;
import javax.ejb.Local;
@Local
public interface StatefullSession1Local extends StatefullInterface
{
// it is blank interface
}

StatefullSession1Remote.java
package techmyguru.stateful;
import javax.ejb.Remote;

@Remote
public interface StatefullSession1Remote extends StatefullInterface 
{
// it is blank interface
}

StatefullSession1.java
package techmyguru.stateful;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.PostActivate;
import javax.ejb.PrePassivate;
import javax.ejb.Remove;
import javax.ejb.Stateful;

@Stateful(mappedName = "statfulljndi1")
public class StatefullSession1 implements StatefullSession1Remote,
  StatefullSession1Local {

ArrayList<String> items;
String user;

// Constructor 
public StatefullSession1() {
  System.out.println("It is constructror ");
}

public void setUser(String user) {
  this.user = user;
}

public String getUser() {
  return user;
}
// it is used to add items in ArrayList 
@Override
public String addItem(String item) {
  items.add(item);
  System.out.println("Item added");
  return "Item added in list" + item;
}
// it is used o delete item from ArrayList
@Override
public String deleteItem(String item) {
  System.out.println("Item deleted");
  if (items.remove(item== true)
    return "Item deleted " + item;
  else
    return "Item not found " + item;
}

// it is used to get all items from ArrayList
@Override
public ArrayList<String> getItems() {
  System.out.println("get items");
  return items;
}

// it is used to destroy stateful component
@Remove
public void destroy() {
  if (items != null) {
    items.removeAll(items);
    items = null;
  }
}

// Lifecycle callback method
@PostConstruct
public void postConstruct() {
  items = new ArrayList<String>();
  user = new String();
  System.out.println("It is Post construct in Statefull ");
}

// Lifecycle callback method
@PreDestroy
public void preDestroy() {
  items = null;
  System.out.println("It is pre destroy in Stateful ");
}

// Lifecycle callback method
@PrePassivate
public void prePassivate() {
  System.out.println("It is pre Passivate in Statefull");
}

// Lifecycle callback method
@PostActivate
public void postActivate() {
  System.out.println("It is pre Post Activate in Statefull");
}
}

Step 7: Add EJB Project to Web project
Right Click on Web Project - > Properties - > Java Build Path -> Click Projects -> Add -> Select "StatefulDemo1EJB".
Stateful Session Bean

Step 8: Create a class to find stateful component
EJBLookup.java
package techmyguru.web;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import techmyguru.stateful.StatefullSession1Remote;

public class EJBLookup 
{
public static StatefullSession1Remote getEjb(String user
{
StatefullSession1Remote remote1 = null;
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");

env.put(Context.PROVIDER_URL, "t3://localhost:7001");
env.put(Context.SECURITY_PRINCIPAL, "weblogic");
env.put(Context.SECURITY_CREDENTIALS, "12345678");
Context ctx = new InitialContext(env);
remote1 = (StatefullSession1Remotectx.lookup
("statfulljndi1#techmyguru.stateful.StatefullSession1Remote");
remote1.setUser(user);
catch (Exception e) {
}
return remote1;
}
}

Step 9: Create index.jsp in Web project
<%@page import="java.util.ArrayList"%>
<%@page import="techmyguru.stateful.StatefullSession1Remote"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<h1> Stateful Session Bean Demo</h1>
<%
Object obj1=session.getAttribute("remote1");
if(obj1!=null)
{
%>
<form action="AddItemServlet" method="post">
Enter Item <input type="text" name="item">
<input type="submit" value="Add Item" name="login">
</form>
<%

StatefullSession1Remote remote1 = (StatefullSession1Remoteobj1;
out.println("Welcome to "+remote1.getUser()+"<br>");
ArrayList<String> list = remote1.getItems();
//out.println(list);
for(String str:list)
{
out.println(str+"<a href=DeleteServlet?item="+str+">[X] Delete</a><br>");
}
out.println("<br><a href='LogoutServlet'>Logout</a>");
}
else
{
%>
<form action="AddItemServlet" method="post">
Enter id : <input type="text" name="uid"><br>
Enter password : <input type="password" name="pass"><br>
<input type="submit" value="Login" name="login">
<input type="reset" >
</form>
<%
}
%>
</body>
</html>

Step 10: Create Servlets in Web project to use Stateful session bean
AddItemServlet.java
package techmyguru.web;
// all import statements
import techmyguru.stateful.StatefullSession1Remote;
public class AddItemServlet extends HttpServlet 
{
private static final long serialVersionUID = 1L;
private StatefullSession1Remote remote1 = null;

protected void doPost(HttpServletRequest request, HttpServletResponse response
throws 
ServletException, IOException 
{
HttpSession session = request.getSession();
PrintWriter pw = response.getWriter();
Object obj1 = session.getAttribute("remote1");
if (obj1 != null
{
remote1 = (StatefullSession1Remoteobj1;
remote1.addItem(request.getParameter("item"));

else 
{
remote1 = EJBLookup.getEjb(request.getParameter("uid"));      
session.setAttribute("remote1", remote1);
}
response.sendRedirect("index.jsp");
}
}

DeleteServlet.java
package techmyguru.web;
// all import statements
public class DeleteServlet extends HttpServlet 
{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response
throws 
ServletException, IOException 
{
HttpSession session = request.getSession();
if (session.isNew()) {

else 
{
Object obj1 = session.getAttribute("remote1");
StatefullSession1Remote remote1 = (StatefullSession1Remoteobj1;
remote1.deleteItem(request.getParameter("item"));
}
response.sendRedirect("index.jsp");
}
}

LogoutServlet.java
package techmyguru.web;
// all import statements
public class LogoutServlet extends HttpServlet 
{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response
throws 
ServletException, IOException 
{
HttpSession session = request.getSession();
if (session.isNew()) {

else 
{
Object obj1 = session.getAttribute("remote1");
StatefullSession1Remote remote1 = (StatefullSession1Remoteobj1;
remote1.destroy();
session.removeAttribute("remote1");
}
response.sendRedirect("index.jsp");
}
}

Step 10: Verify the directory structure
Stateful Session Bean

Step 11: Run the Application
Right click on Web Project -> Run As -> Run on Server
Stateful Session Bean

Specify id and password and click Login and get below screen :
Stateful Session Bean


Previous Download Source Code Next


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code