java 写一个商店_Java Web开发之基于Session的购物商店实现方法

package cn.com.shopping;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

//完成购买

public class BuyServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String id=request.getParameter("id");

Book book=(Book)Db.getAll().get(id);

//再加上那个关闭Cookie时session的剞劂方案

//阻止session的时候解决方案

HttpSession session=request.getSession(false);

//从session中得到用户的保存所有书的集合(购物车)

List list=(List)session.getAttribute("list");

if(list==null)

{

list=new ArrayList();

session.setAttribute("list", list);

}

list.add(book);

String url=response.encodeRedirectURL("/Session/SessionCountDemo");

response.sendRedirect(url);

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request,response);

}

}

package cn.com.shopping;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.LinkedHashMap;

import java.util.Map;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

//显示书

public class ListBookServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setCharacterEncoding("UTF-8");

response.setContentType("text/html;charset=UTF-8");

PrintWriter out=response.getWriter();

HttpSession session=request.getSession();

out.print("本店有如下的商品:
");

Map map=Db.getAll();

for(Map.Entry entry:map.entrySet())

{

Book book=entry.getValue();

String url=response.encodeURL("/Session/BuyServlet?id="+book.getId());

out.print(book.getName()+"购买
");

}

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request,response);

}

}

//Db作为数据库

class Db

{

private static Map map=new LinkedHashMap();

static

{

map.put("1", new Book("1","Java WEB开发","WY","好书"));

map.put("2", new Book("2","WEB开发","zt","一般"));

map.put("3", new Book("3","程序设计","df","较好书"));

map.put("4", new Book("4","计算机组成","as","一般好书"));

map.put("5", new Book("5","编译原理","ty","很好书"));

map.put("6", new Book("6","网络维护","hj","非常好书"));

}

public static Map getAll()

{

return map;

}

}

//书

class Book

{

private String id;

private String name;

private String author;

private String description;

public Book() {

super();

// TODO Auto-generated constructor stub

}

public Book(String id, String name, String author, String description) {

super();

this.id = id;

this.name = name;

this.author = author;

this.description = description;

}

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;

}

public String getAuthor() {

return author;

}

public void setAuthor(String author) {

this.author = author;

}

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

}

package cn.com.shopping;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

public class SessionCountDemo extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setCharacterEncoding("UTF-8");

response.setContentType("text/html;charset=UTF-8");

PrintWriter out=response.getWriter();

HttpSession session=request.getSession();

if(session==null)

{

out.write("您没买任何的商品!");

return;

}

out.write("您购买了如下的商品:");

List list=(List) session.getAttribute("list");

for(Book book:list)

{

out.write(book.getName());

}

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request,response);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值