JavaEE5学习札记05-EJB之会话Bean(sessionBean)总结-4
以上好像还看不出什么,那么咱们开发一个web程序作为测试用例
建立一个web项目,将jboss的client文件夹下面所有的jar包都拷贝进此web项目中。
Index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head></head> <body> <form name="form1" method="post" action="buy.jsp"> <label> <input type="checkbox" name="items" value="suhuanzhen"> 素还真 </label> <label> <input type="checkbox" name="items" value="yexiaochai"> 叶小钗 </label> <label> <input type="checkbox" name="items" value="heibailangjun"> 黑白郎君 </label> <br> <label> <input type="submit" name="Submit" value="提交"> </label> </form>
</body> </html> |
Buy.jsp
<%@ page language="java" import="java.util.*,javax.naming.*,ejb.sessionBean.*" pageEncoding="utf-8"%> <% ShopService shopService = (ShopService) session .getAttribute("ShopService");
if (shopService == null) {
Context context = null;
String init_factory = "org.jnp.interfaces.NamingContextFactory"; String serverURL = "jnp://127.0.0.1:1099";
Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, init_factory);
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
properties.put(Context.PROVIDER_URL, serverURL);
context = new InitialContext(properties);
shopService = (ShopService) context .lookup("ShopServiceEAOImpl/remote");
session.setAttribute("ShopService", shopService);
}
String[] items = request.getParameterValues("items");
for (String itme : items) { shopService.addItem(itme); }
System.out.println(shopService.showItems()); %> <a href="index.jsp">[重新选购]</a> | <a href="removeBean.jsp">[销毁EJB回话Bean]</a> |
removeBean.jsp
<%@ page language="java" import="java.util.*,javax.naming.*,ejb.sessionBean.*" pageEncoding="utf-8"%> <% ShopService shopService = (ShopService) session .getAttribute("ShopService");
if (shopService == null) {
Context context = null;
String init_factory = "org.jnp.interfaces.NamingContextFactory"; String serverURL = "jnp://127.0.0.1:1099";
Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, init_factory);
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
properties.put(Context.PROVIDER_URL, serverURL);
context = new InitialContext(properties);
shopService = (ShopService) context .lookup("ShopServiceEAOImpl/remote");
session.setAttribute("ShopService", shopService);
} shopService.removeForClient(); session.removeAttribute("ShopService"); %> <a href="index.jsp">[重新选购]</a> |
Index.jsp是选购人物页面,buy.jsp是调用业务处理逻辑,removeBean是显示的让服务器销毁会话Bean对象。将此项目发布到tomcat中运行,效果如下
选中“素还真”提交
客户端控制台如下
{suhuanzhen=1} |
之后在选择素还真、黑白郎君
控制台如下
{suhuanzhen=2, heibailangjun=1} |
之后在选择素还真、叶小钗、黑白郎君
控制台如下
{suhuanzhen=3, yexiaochai=1, heibailangjun=2} |
我们再看看服务端的控制台,以上3个操作造成了控制台信息如下
11:54:20,279 INFO [STDOUT] 有状态SessionBean:ShopServiceEAOImpl构造完毕 11:54:20,791 INFO [STDOUT] buyInfo:{suhuanzhen=1} 11:55:27,100 INFO [STDOUT] buyInfo:{suhuanzhen=2, heibailangjun=1} 11:56:07,511 INFO [STDOUT] buyInfo:{suhuanzhen=3, yexiaochai=1, heibailangjun=2} |
之后让另一台计算机或者另一个浏览器——火狐访问你这个tomcat的web应用,重新选择素还真控制台“累计”信息如下
{suhuanzhen=1} {suhuanzhen=2, heibailangjun=1} {suhuanzhen=3, yexiaochai=1, heibailangjun=2} {suhuanzhen=1} |
之后本机再次重新选择素还真,控制台信息如下
{suhuanzhen=1} {suhuanzhen=2, heibailangjun=1} {suhuanzhen=3, yexiaochai=1, heibailangjun=2} {suhuanzhen=1} {suhuanzhen=4, yexiaochai=1, heibailangjun=2} |
由此观之,就像HttpSession一样,有状态的SessionBean各自维护自己客户端的实例的变量状态。不同的客户端,sessionBean的实例变量不会互相干扰,各干各的。当然,如果调用了远程方法,销毁了有状态的会话Bean的时候也要将您httpSession中的会话对象删除掉,这样才能够让远程SessionBean重新创建对象和实例变量啊。