07120170909
1、session是在cookie的基础之上,利用cookie返回JSESSIONID(key[服务器随机生成])存在客户端实现,正真的数据存在服务端[key-value]。
2、session的使用步骤:
1)、设置:request.getSession().setAttribute("username", username);
2)、获取:request.getSession(false).getAttribute(username); 注:[false表示沿用之前设置的;不设置,默认为true,如果存在session,则沿用之前的,没有则新建];
3、session是每个浏览器对应一个JSESSIONID; --->注:例如设置session之后,在Resquest Headers 会存在Cookie:JSESSIONID=A75D52D01876F3B404B8CC668BC8180C
4、代码如下:
package com.tiger.two0906.bean;
import java.io.*;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
/**
* 水果保存到购物车
* @author tiger
* @time 2017年9月9日
*/
@WebServlet("/fruitSave")
public class FruitSave extends HttpServlet {
private static final long serialVersionUID = 1L;
public FruitSave() { }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List
fruit_list = (List
) request.getServletContext().getAttribute("fruit_list");
String[] ids = request.getParameterValues("sg");
List
fruits = new ArrayList<>();
for (Fruit fruit : fruit_list) {
Integer id = fruit.getId();
for (String d : ids) {
if (id==Integer.parseInt(d)) {
fruits.add(fruit);
}
}
}
//将信息添加到session中,不同的客户拥有不同的水果列表
request.getSession().setAttribute("fruits", fruits);
response.sendRedirect("shopCar");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.tiger.two0906.bean;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/shopCar")
public class FruitCar extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
List
fruits = (List
) request.getSession().getAttribute("fruits"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println("
");
out.println("---shopCar---");
out.println(""); out.println(""); out.println(""); out.println("
"); for (Fruit fruit : fruits) { out.println( "
" + "
"+fruit.getId()+"" + "
"+fruit.getName()+"" + "
"+fruit.getPrice()+"" + "
"+fruit.getCount() +"" + "
"); } out.println("
"); out.println(""); out.println(""); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } package com.tiger.two0906.bean; /** * 水果bean * @author tiger * @time 2017年9月6日 */ public class Fruit { Integer id; String name; double price; int count; public void setId(Integer id) { this.id = id; } public Integer getId() { return 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; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public Fruit() { } public Fruit(Integer id, String name, double price, int count) { super(); this.id = id; this.name = name; this.price = price; this.count = count; } @Override public String toString() { return "Fruits [id=" + id + ", name=" + name + ", price=" + price + ", count=" + count + "]"; } } package com.tiger.two0906.bean; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; /** * 07120170909 * 水果列表 * @author tiger * @time 2017年9月6日 */ @WebServlet("/shopList") public class FruitList extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); //数据源 Fruit[] fruits = {new Fruit(1,"苹果",2.32, 22), new Fruit(2,"雪梨",2.32, 22), new Fruit(3,"坚果",2.32, 22), new Fruit(4,"苹果",2.32, 22), new Fruit(5,"葡萄",2.32, 22)}; //将数据源添加进List集合 List
fruit_list = new ArrayList
(); Collections.addAll(fruit_list, fruits); request.getServletContext().setAttribute("fruit_list", fruit_list); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println("
");
out.println("---shopList---");
out.println(""); out.println(""); out.println(""); out.println("
");
out.println("
");
out.println("
");
out.println("
选择");
out.println("
水果名");
out.println("
价格");
out.println("
数量");
out.println("
");
for (Fruit fruit : fruit_list) {
out.println( "
"
+ "
"+fruit.getName()+""
+ "
"+fruit.getPrice()+""
+ "
"+fruit.getCount() +"" );
}
out.println("
");
out.println("
");
out.println("");
out.println("
");
out.println("
");
out.println("
");
out.println("");
out.println("");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
}