EJB>Session Beans 小强斋

一、 无状态Session Bean

第一步:要定义一个会话Bean,首先需要定义一个包含他所有业务方法的接口。

package com.foshanshop.ejb3;
public interface HelloWorld {
public String SayHello(String name);
}

第二步:实现上面的接口并加入两个注释@Stateless , @Remote,第一个注释定义这是一个无状态会话Bean,第二个注释指明这个无状态Bean 的remote 接口。

package com.foshanshop.ejb3.impl;
import com.foshanshop.ejb3.HelloWorld;
import javax.ejb.Remote;
import javax.ejb.Stateless;
@Stateless
@Remote ({HelloWorld.class})
public class HelloWorldBean implements HelloWorld {
public String SayHello(String name) {
return name +"说:你好!世界,这是我的第一个EJB3哦.";
}
}

test.jsp

<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="com.foshanshop.ejb3.HelloWorld, javax.naming.*, java.util.Properties"%>
<%
Properties props = new Properties();

props.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "localhost:1099");
props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
InitialContext ctx;
try {
ctx = new InitialContext(props);//如果客户端和jboss运行在同一个jvm,不需要传入props
HelloWorld helloworld = (HelloWorld) ctx.lookup("HelloWorldBean/remote");
out.println(helloworld.SayHello("佛山人"));
} catch (NamingException e) {
out.println(e.getMessage());
}
%>

开发只存在Local 接口的无状态会话Bean 的步骤和上节开发只存在Remote 接口的无状态会话Bean 的步骤相同,
两者唯一不同之处是,前者下面是只存在Local 接口的无状态会话Bean 代码。

二、有状态Bean

有状态Bean 是一个可以维持自身状态的会话Bean。每个用户都有自己的一个实例,在用户的生存期内,Stateful
Session Bean 保持了用户的信息,即“有状态”;一旦用户灭亡(调用结束或实例结束),Stateful Session Bean 的
生命期也告结束。即每个用户最初都会得到一个初始的Stateful Session Bean。
Stateful Session Bean 的开发步骤与Stateless Session Bean 的开发步骤相同。
先定义接口
package com.foshanshop.ejb3;
package com.foshanshop.ejb3;

import java.io.Serializable;
import java.util.List;

public interface Cart extends Serializable {
  public void AddBuyItem(String productName);
  public List<String> getBuyItem();
}

stateful session bean 必须实现Serializable 接口,这样EJB 容器才能在她们不再使用时序列化存储她们的状态信息.

package com.foshanshop.ejb3.impl;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateful;
import com.foshanshop.ejb3.Cart;

@SuppressWarnings("serial")
@Stateful
@Remote(Cart.class)
public class CartBean implements Cart{
 private List<String> buyitem = new ArrayList<String>();
 
 public void AddBuyItem(String productName) {
  buyitem.add(productName);
 }

 public List<String> getBuyItem() {
  return buyitem;
 }

}

通过@Stateful 注释定义这是一个有状态会话Bean,@Remote注释指明有状态Bean 的remote 接口。@SuppressWarnings("serial") 注释屏蔽缺少serialVersionUID 定义的警告。

StatefulBeanTest.jsp
<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="com.foshanshop.ejb3.*,java.util.*,javax.naming.*"%>
<%
	try {
		InitialContext ctx = new InitialContext();
		Cart cat = (Cart)session.getAttribute("cat");
		if(cat==null){//创建一个购物车
			cat = (Cart) ctx.lookup("CartBean/remote");
			session.setAttribute("cat", cat);
		}
		cat.AddBuyItem("《EJB3.0实例教程》");
		List<String> buyitem = cat.getBuyItem();
		out.println("购物车的商品列表:<br>");
		for(String name : buyitem){
			out.println("  "+ name+ "<br>");
		}		
	} catch (Exception e) {
		out.println(e.getMessage());
	}
%>

原文地址:https://www.cnblogs.com/xiaoqiangzhaitai/p/5637701.html