HttpSession
@SuppressWarnings("serial")
public class Demo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
HttpSession session = request.getSession();
session.setAttribute("username", username);
System.out.println(session.getId());
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
public class Demo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
HttpSession session = request.getSession();
String username = (String)session.getAttribute("username");
response.getWriter().write(username + " " + session.getId());
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
public class ShowCar extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
HttpSession session = request.getSession();
ArrayList<Book> car = (ArrayList<Book>) session.getAttribute("car");
PrintWriter out = response.getWriter();
if (car != null) {
for (Book book : car) {
out.write(book + "<br>");
}
out.write("<a href='" + request.getContextPath() + "/showallbooks'>会到商品页面</a>");
} else {
out.write("当前无任何商品");
response.setHeader("refresh", "3;url=" + request.getContextPath() + "/showallbooks");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
public class ShowAllBooks extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
HashMap<String, Book> books = BookUtil.getBooks();
for (String key : books.keySet()) {
Book book = books.get(key);
out.write("<a href='" + request.getContextPath() + "/addbook?id=" + book.getId() + "' name="
+ book.getName() + ">" + book + "</a><br>");
}
out.write("<a href='" + request.getContextPath() + "/showcar'>查看购物车</a>");
request.getSession().setMaxInactiveInterval(10);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
public class BookUtil {
private static HashMap<String, Book> books = new HashMap<>();
static {
books.put("1", new Book("1", "降龙十八掌"));
books.put("2", new Book("2", "打狗棒法"));
books.put("3", new Book("3", "九阴真经"));
books.put("4", new Book("4", "九阳神功"));
}
public static HashMap<String, Book> getBooks() {
return books;
}
public static Book findBookById(String id) {
return books.get(id);
}
}
public class Book implements Serializable {
/**
* 序列化 id
*/
private static final long serialVersionUID = 1L;
private String id;
private String name;
public Book() {
super();
}
public Book(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return id + " " + name;
}
}
public class AddBook extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
Book book = BookUtil.findBookById(id);
HttpSession session = request.getSession();
ArrayList<Book> car = (ArrayList<Book>)session.getAttribute("car");
if (car == null) {
car = new ArrayList<>();
}
car.add(book);
session.setAttribute("car", car);
response.setHeader("refresh", "2;url=" + request.getContextPath() + "/showallbooks");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
http://blog.csdn.net/huzongnan/article/list