session的用法和在项目中的应用

在我所做 的项目中用到session的地方


1。用户的登录/退出

2。购物车


在jsp+servelt+javabean的mvc的开发模式下的应用:只粘贴servlet代码了:

登录:
if (user != null) {
HttpSession session = request.getSession(true);
session.setAttribute("user", user);

ShopCart cart = new ShopCart();
session.setAttribute("cart", cart);
response.sendRedirect("../product/products1.jsp");

}else {
request.setAttribute("msg", "此用户不存在");
this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
}



用户退出:

HttpSession session=request.getSession(false);
session.removeAttribute("cart");
session.removeAttribute("user");
session.invalidate();
response.sendRedirect("../index.jsp");



购物车:


public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String option=request.getParameter("option");
		
		
		System.out.println("-------------"+option);
		if(option!=null&&!"".equals(option)){
			HttpSession session=request.getSession(false);
			ShopCart cart=(ShopCart)session.getAttribute("cart");
			
			if(cart==null){
				cart=new ShopCart();
				session.setAttribute("cart", cart);
			}
			 String pid=request.getParameter("pid");
						int id=Integer.parseInt(pid);
			
						ProductDAO pdao=new ProductDAOimpl();
			if("add".equals(option)){
				Product product=pdao.getProductById(id);
				cart.addProduct(product);
				//this.getServletContext().getRequestDispatcher("/servlet/ProductServlet?option=userfind").forward(request, response);
				
				
				
			}else if("delet".equals(option)){
				
				cart.deletProduct(id);
				this.getServletContext().getRequestDispatcher("/product/cartshowyk.jsp").forward(request, response);
				
				
				
				
		
				
				
			}else if("update".equals(option)){
				String number2=request.getParameter("number2");
				int number=Integer.parseInt(number2);
				cart.updateProductNumber(id, number);
				this.getServletContext().getRequestDispatcher("/product/cartshowyk.jsp").forward(request, response);
				
				
				
				
			}
		}

	}



----------------------------------------------------------------------下面只貼 在struts2 中获取 session,reuqest,response的方法:

1.
非IoC(Spring中的控制反转)方式:


直接使用ServletActionContext类的getRequest、getResponse方法来获得HttpServletRequest、HttpServletResponse对象。代码如下:
   
 HttpServletRequest request=ServletActionContext.getRequest();
 HttpServletResponse response = ServletActionContext.getResponse();

Map session=ActionContext.getContext().getSession();





2.IoC方式:
public class BaseAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {   
  
   
       
    ActionContext context = ActionContext.getContext();   
    HttpServletRequest request;   
    HttpServletResponse response;   
    SessionMap session;   
public void setSession(Map map) {   
        this.session = (SessionMap) map;   
    }   
    public void setServletRequest(HttpServletRequest request) {   
        this.request = request;   
    }   
    public void setServletResponse(HttpServletResponse response) {   
        this.response = response;   
    }   
       
}  





三。项目中session的用法


1.session的创建

Map session = ActionContext.getContext().getSession();
session.put("usr", usr);
2.通过session获取值
在jsp页面中:

<s:property value="#session.usr.username"/>


3.session的销毁
Map session = ActionContext.getContext().getSession();
session.remove("usr");
session.clear();

4.然后在jsp中取值:
<s:property value="#session.user.username"/>




你可能感兴趣的:(spring,jsp,mvc,servlet,IOC)