Java Web开发之基于Session的购物商店实现方法
作者:烟大洋仔 时间:2023-06-09 20:46:35
本文实例讲述了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("本店有如下的商品:<br/>");
Map<String ,Book > map=Db.getAll();
for(Map.Entry<String, Book> entry:map.entrySet())
{
Book book=entry.getValue();
String url=response.encodeURL("/Session/BuyServlet?id="+book.getId());
out.print(book.getName()+"<a href='"+url+"' target='_blank' >购买</a><br/>");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
//Db作为数据库
class Db
{
private static Map<String ,Book> 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<Book> 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);
}
}
希望本文所述对大家Java web程序设计有所帮助。
标签:Java,Session
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android后端服务器的搭建方法
2022-05-07 19:38:04
![](https://img.aspxhome.com/file/2023/6/113346_0s.png)
java文件操作练习代码 读取某个盘符下的文件
2023-11-12 06:34:07
详解SpringMVC重定向传参数的实现
2022-09-20 19:01:02
![](https://img.aspxhome.com/file/2023/0/91480_0s.png)
C# webclient中文乱码问题解决方法
2022-06-02 16:39:43
解决Springboot2.1.x配置Activiti7单独数据源问题
2022-07-28 02:24:04
C#实战之备忘录的制作详解
2023-08-13 02:17:21
![](https://img.aspxhome.com/file/2023/8/116638_0s.jpg)
Android Studio finish()方法的使用与解决app点击“返回”(直接退出)
2022-07-31 09:37:10
![](https://img.aspxhome.com/file/2023/9/137809_0s.jpg)
SpringBoot中获取profile的方法详解
2023-11-19 18:05:00
android实现扫码枪功能
2022-08-28 21:33:35
java集合中list的用法代码示例
2023-04-11 23:29:17
android通过Location API显示地址信息的实现方法
2021-09-16 15:41:02
![](https://img.aspxhome.com/file/2023/5/139525_0s.png)
Android自定义控件属性详细介绍
2022-09-03 09:47:02
Java中Lambda表达式的进化之路详解
2023-04-19 21:17:42
Java组件commons fileupload实现文件上传功能
2022-05-03 15:03:07
![](https://img.aspxhome.com/file/2023/7/85527_0s.jpg)
java中LinkedList使用迭代器优化移除批量元素原理
2021-12-05 11:26:07
![](https://img.aspxhome.com/file/2023/0/66180_0s.png)
C#中XML基础用法
2023-08-05 17:12:34
Android实现通讯录效果——获取手机号码和姓名
2021-10-25 11:53:59
![](https://img.aspxhome.com/file/2023/1/118961_0s.gif)
Java中stream处理中map与flatMap的比较和使用案例
2023-11-21 02:27:53
SpringIOC容器Bean的作用域及生命周期实例
2023-10-01 11:21:34
![](https://img.aspxhome.com/file/2023/1/76611_0s.png)
详解Java的回调机制
2023-07-27 07:17:43
![](https://img.aspxhome.com/file/2023/7/60657_0s.png)