📄 cartsessionlistener.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package cart.servlet;import ejb3.day2.ShoppingCart;import javax.ejb.EJB;import javax.naming.Context;import javax.naming.InitialContext;import javax.servlet.http.HttpSession;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;/** * Web application lifecycle listener. * @author user */@EJB(name="ejb/cart", beanInterface=ShoppingCart.class, beanName="ShoppingCartBean")public class CartSessionListener implements HttpSessionListener { public void sessionCreated(HttpSessionEvent arg0) { try{ Context ctx = new InitialContext(); ShoppingCart cart = (ShoppingCart)ctx.lookup("java:comp/env/ejb/cart"); HttpSession ses = arg0.getSession(); ses.setAttribute("cart", cart); }catch(Exception e){ throw new RuntimeException(e); } } public void sessionDestroyed(HttpSessionEvent arg0) { HttpSession ses = arg0.getSession(); ses.removeAttribute("cart"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -