1.服务器端在每接收到一次request请求时,都建立一个request对象,
2.服务器端在第一次和某client端建立会话时(可以http请求为标识)建立Session对象,并在Session的存活时间内留在内存中,当结束会话时(如关掉IE浏览器)或其存活时间过期时Session对象消亡
3.Application对象在启动服务器建立,服务器关闭时消亡
用MVC思想实现购物车
//Control.java实现控制
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import cart.*;
import java.util.*;
/**
* Servlet implementation class for Servlet: Control
*
*/
public class Control extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
MyDb md=new MyDb();
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public Control() {
super();
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String oper=request.getParameter("oper");
String _id=request.getParameter("id");
int id=-1;
if(_id!=null)
id=Integer.parseInt(_id);
HttpSession session=request.getSession();
ShoppingCart sc=(ShoppingCart)session.getAttribute("cart");
if(sc==null){
sc=new ShoppingCart();
session.setAttribute("cart",sc);
}
if("view".equals(oper)){
request.getRequestDispatcher("/showCart.jsp").forward(request,response);
return;
}
if("buy".equals(oper)){
sc.addItem(md.getById(id));
request.getRequestDispatcher("/showCart.jsp").forward(request,response);
return;
}
List aL=md.getAll();
request.setAttribute("items",aL);
request.getRequestDispatcher("/showItems.jsp").forward(request,response);
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
//Item.java商品类
package cart;
public class Item {
private int id;
private String name;
private float price;
public Item(){}
public Item(int id,String name,float price){
this.id=id;
this.name=name;
this.price=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 float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String toString(){
return "[id:"+id+",name:"+name+",price:"+price+"]";
}
}
//MyDb.java作为数据源
package cart;
import java.util.*;
public class MyDb {
List aL=new ArrayList();
public MyDb(){
aL.add(new Item(1,"Apple",10));
aL.add(new Item(2,"Banana",20));
aL.add(new Item(3,"Orange",30));
aL.add(new Item(4,"Pear",10));
}
public List getAll(){
return aL;
}
public Item getById(int id){
for(int i=0;i<aL.size();i++){
Item item=(Item)aL.get(i);
if(item.getId()==id)return item;
}
return null;
}
}
//ShoppingCart.java实现购物操作
package cart;
import java.util.*;
public class ShoppingCart {
private List aL=new ArrayList();
public void empty(){
aL.clear();
}
public void addItem(Item item){
aL.add(item);
}
public void removeItem(int id){
for(int i=0;i<aL.size();i++){
Item item=(Item)aL.get(i);
if(item.getId()==id){
aL.remove(i);
return;
}
}
}
public List getAllItem(){
return aL;
}
}
//ShowItems.jsp显示商品页面
<body>
<%
List aL=(List)request.getAttribute("items");
for(int i=0;i<aL.size();i++){
Item item=(Item)aL.get(i);
out.println("<a href='?oper=buy&id="+item.getId()+"'>"+item.getName()+"</a>");
out.println("价钱:"+item.getPrice()+"<br/>");
}
%>
<br/>
<a href="?oper=view">查看购物车</a>
</body>
//ShowCart.jsp显示购物车页面
<body>
<%
ShoppingCart sc=(ShoppingCart)session.getAttribute("cart");
List aL=sc.getAllItem();
for(int i=0;i<aL.size();i++){
Item item=(Item)aL.get(i);
out.println("<a href='?oper=buy&id="+item.getId()+"'>"+item.getName()+"</a>");
out.println("价钱:"+item.getPrice()+"<br/>");
}
%>
<a href="Control">继续购物</a>
</body>