stateful session bean 学习笔记
利用stateful session bean 完成一个简单的购物车
1、建立ejbProject将要完成的业务抽象到远程接口中CartRemote:
package net.hnspi.ejb;import java.util.Map;import javax.ejb.Remote;@Remotepublic interface CartRemote {public ShopItem addCart(ShopItem si) ;public Map<ShopItem,Integer> itemInCart() ;}
package net.hnspi.ejb;import java.util.HashMap;import java.util.Map;import javax.ejb.Stateful;@Statefulpublic class CartBean implements CartRemote { public CartBean(){} //private Map<ShopItem,Integer> items = new HashMap<ShopItem,Integer>() ; private Map<ShopItem,Integer> items ;@Overridepublic ShopItem addCart(ShopItem si) {if(itemInCart()==null){items = new HashMap<ShopItem,Integer>() ;}int temp = 0 ;if(items.containsKey(si)){temp = items.get(si) ;items.put(si, ++temp) ;}else{items.put(si, 1) ;}return si;}@Overridepublic Map<ShopItem,Integer> itemInCart() {return this.items;}}
package net.hnspi.ejb;import java.io.Serializable;public class ShopItem implements Serializable{private static final long serialVersionUID = 1L;private int id ;private String name ;private double price ;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}/** * 复写equals约定名称相同就为同一件商品 */@Overridepublic boolean equals(Object obj) { if(this==obj){return true ;}if(!(obj instanceof ShopItem)){return false ;}ShopItem s = (ShopItem)obj ;if(this.name.equals(s.name)){return true ;}else{return false ;}}@Overridepublic int hashCode() { return this.name.hashCode()+5 ;}}
package net.hnspi.util;import java.util.Properties;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;import net.hnspi.ejb.CartRemote;public class ContextManager {/*public static void main(String[] args) {Context cxt = getJbossContext() ;CartRemote cr = null;try {cr = (CartRemote)cxt.lookup("Shop/CartBean/remote");} catch (NamingException e) {e.printStackTrace();}System.out.println(cr);}*/private static CartRemote cr ;public static Context getJbossContext(){Properties pro = new Properties() ;pro.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory") ;pro.put(Context.PROVIDER_URL, "localhost:1099") ;Context cxt = null;try {cxt = new InitialContext(pro);} catch (NamingException e) {e.printStackTrace();}return cxt ;}public static CartRemote getInstance(){if(cr==null){try {cr = (CartRemote)getJbossContext().lookup("Shop/CartBean/remote");} catch (NamingException e) {e.printStackTrace();}}return cr ;}}
package net.hnspi.servlet;import java.io.IOException;import java.util.Map;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.hnspi.ejb.CartRemote;import net.hnspi.ejb.ShopItem;import net.hnspi.util.ContextManager;public class ShopServlet extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request, response) ;}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {CartRemote cr = ContextManager.getInstance() ;ShopItem si = null ;String[] items = request.getParameterValues("item") ;for(String s : items){si = new ShopItem() ;si.setName(s) ;cr.addCart(si) ;}Map<ShopItem,Integer> map = cr.itemInCart();request.setAttribute("items", map) ;request.getRequestDispatcher("display.jsp").forward(request, response) ;}}//不要忘了在web.xml中进行servlet的配置
<?xml version="1.0" encoding="UTF-8" ?><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><base href="<%=basePath%>" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>shopItem_list</title></head><body><fieldset><legend>商品列表</legend><form action="shopServlet.do"><input type="checkbox" name="item" value="struts" />Struts<br /><input type="checkbox" name="item" value="hibernate" />Hibernate<br /><input type="checkbox" name="item" value="spring" />Spring<br /><input type="submit" value="提交" /></form></fieldset></body></html>
<?xml version="1.0" encoding="UTF-8" ?><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ page import="java.util.*,net.hnspi.ejb.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><base href="<%=basePath%>" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>title</title></head><body><%Map<ShopItem,Integer> items = (Map<ShopItem,Integer>)request.getAttribute("items") ;for(Map.Entry<ShopItem,Integer> me : items.entrySet()){%>商品名称:<%=me.getKey().getName() %>----数量:<%=me.getValue() %><br /><%}%></body></html>