利用stateful session bean 完成一个简单的购物车
1、建立ejbProject将要完成的业务抽象到远程接口中CartRemote:
package net.hnspi.ejb; import java.util.Map; import javax.ejb.Remote; @Remote public interface CartRemote { public ShopItem addCart(ShopItem si) ; public Map<ShopItem,Integer> itemInCart() ; }
2、远程接口的实现类CartBean:
package net.hnspi.ejb; import java.util.HashMap; import java.util.Map; import javax.ejb.Stateful; @Stateful public class CartBean implements CartRemote { public CartBean(){} //private Map<ShopItem,Integer> items = new HashMap<ShopItem,Integer>() ; private Map<ShopItem,Integer> items ; @Override public 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; } @Override public Map<ShopItem,Integer> itemInCart() { return this.items; } }
3、商品的pojo类ShopItem:
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约定名称相同就为同一件商品 */ @Override public 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 ; } } @Override public int hashCode() { return this.name.hashCode()+5 ; } }
4、建立要访问ejb组件的web项目,在项目建立一个区的同一个ejb代理对象的工具类ContextManager:
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 ; } }
5、对请求购买的商品进行处理的servlet ShopServlet:
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的配置
6、进行商品购买的jsp页面shopitem_list.jsp
<?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>
7、进行商品显示的页面display.jsp
<?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>
学习书籍:经典java ee企业应用实战