Servlet第五节整理

购物界面代码:

//productList
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession hs=request.getSession();
		String uid=(String) hs.getAttribute("uid");
		if(uid==null){
			response.sendRedirect("index.jsp");
		}
		
//		建立一个List来客串数据库里的数据
		List<Book> blist=new ArrayList<Book>();
		Book b1=new Book(1,"三国",30);
		Book b2=new Book(2,"水浒",40);
		Book b3=new Book(3,"红楼",50);
		blist.add(b1);
		blist.add(b2);
		blist.add(b3);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw=response.getWriter();
		
		pw.println("<table>");
		for(Book b:blist){
			pw.println("<tr>");
			pw.println("<td>"+b.getId()+"</td>");
			pw.println("<td>"+b.getName()+"</td>");
			pw.println("<td>"+b.getPrice()+"</td>");
			String path=response.encodeURL("add.do?id="+b.getId()+"&name="+b.getName()+"&price="+b.getPrice());
//			String path=response.encodeURL("<a href='add.do'>购买</a>");
//			String path=response.encodeURL("add.do?id=1&name=三国&price=50");
			
			System.out.println(path);
			pw.println("<td><a href='"+path+"'>购买</a></td>");
			pw.println("</tr>");
		}		
		
		pw.println("</table>");
		pw.close();
	}
//Book 界面
package com.gouwu;

public class Book {
	private int id;
	private String name;
	private double price;
	
	public Book(int id, String name, double price) {
		super();
		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 double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		long temp;
		temp = Double.doubleToLongBits(price);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Book other = (Book) obj;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
			return false;
		return true;
	}
}

//ADD界面
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession hs=request.getSession();
//		让session立即超时失效
//		hs.invalidate();
		try{
			String uid=(String) hs.getAttribute("uid");
			System.out.println(uid);
			if(uid==null){
				response.sendRedirect("index.jsp");
				return;
			}
			Map<Book,Integer> cartmap=(Map<Book, Integer>) hs.getAttribute("cartmap");
//			如果carmap是null,说明用户首次来购物
			if(cartmap==null){
				cartmap=new HashMap<Book,Integer>();
			}
			
//			建立一个书籍对象来存储传递过来的要购买的书籍属性参数
			int id=Integer.parseInt(request.getParameter("id"));
			String name=request.getParameter("name");
			double price=Double.parseDouble(request.getParameter("price"));
			
			Book b=new Book(id,name,price);
//			把书籍填到cartmap里
//			添加之前先判断是否有同类型书籍存在
			Integer mount=cartmap.get(b);
			if(mount==null){
				cartmap.put(b, 1);
			}else{
				cartmap.put(b, ++mount);
			}
			
//			把存有商品的购物车集合carmap加入到session中
			hs.setAttribute("cartmap", cartmap);
			
//			跳转页面到购物车页面
			String path=response.encodeURL("cartview");
			response.sendRedirect(path);
			
		}catch(IllegalStateException e){
			e.printStackTrace();
			response.sendRedirect("index.jsp");
		}	
	}
//CartView界面
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession hs=request.getSession();
		String uid=(String) hs.getAttribute("uid");
		if(uid==null){
			response.sendRedirect("index.jsp");
			return;
		}
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw=response.getWriter();
		
		Map<Book,Integer> cartmap=(Map<Book, Integer>) hs.getAttribute("cartmap");
		Set<Entry<Book,Integer>> es=cartmap.entrySet();
		
		pw.println("<table>");
		for(Entry<Book,Integer> en:es){
			Book b=en.getKey();
			int mount=en.getValue();
			pw.println("<tr>");
			pw.println("<td>"+b.getId()+"</td>");
			pw.println("<td>"+b.getName()+"</td>");
			pw.println("<td>"+b.getPrice()+"</td>");
			pw.println("<td>"+mount+"</td>");
			String path=response.encodeURL("assure.do?id="+b.getId()+"&name="+b.getName()+"&price="+b.getPrice()+"&mount="+mount);
			pw.println("<td><a href='"+path+"'>结算确认</a></td>");
			pw.println("</tr>");
		}		
		
		pw.println("</table>");
		String path=response.encodeURL("ProductList");
		pw.println("<a href='"+path+"'>继续购买</a>");
		pw.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
//Assure界面
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession hs=request.getSession();
		String uid=(String) hs.getAttribute("uid");
		if(uid==null){
			response.sendRedirect("index.jsp");
			return;
		}
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw=response.getWriter();
//		获得要结算的商品信息
		int id=Integer.parseInt(request.getParameter("id"));
		String name=request.getParameter("name");
		double price=Double.parseDouble(request.getParameter("price"));
		int mount=Integer.parseInt(request.getParameter("mount"));
		
		pw.println("要结算的商品信息如下:");
		pw.println("<table>");
		pw.println("<tr>");
		pw.println("<th>商品编号</td>");
		pw.println("<td>商品名称</td>");
		pw.println("<td>商品价格</td>");
		pw.println("<td>商品数量</td>");
		pw.println("<td>总价格</td>");
		pw.println("</tr>");
		pw.println("<tr>");
		pw.println("<td>"+id+"</td>");
		pw.println("<td>"+name+"</td>");
		pw.println("<td>"+price+"</td>");
		pw.println("<td>"+mount+"</td>");
		pw.println("<td>"+(mount*price)+"</td>");
		pw.println("</tr>");
		pw.println("</table>");
		String path=response.encodeURL("deal.do?id="+id+"&name="+name+"&price="+price+"&mount="+mount);
		pw.println("<a href='"+path+"'>结算</a>");
		pw.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
//结算界面
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		结算操作本质上是将已付款产品从购物车中删除
		HttpSession hs=request.getSession();
		String uid=(String) hs.getAttribute("uid");
		if(uid==null){
			response.sendRedirect("index.jsp");
			return;
		}
		Map<Book,Integer> cartmap=(Map<Book, Integer>) hs.getAttribute("cartmap");
//		根据传进参数构建要删除对象作为键
		int id=Integer.parseInt(request.getParameter("id"));
		String name=request.getParameter("name");
		double price=Double.parseDouble(request.getParameter("price"));
		Book b=new Book(id,name,price);
//		以数量作为值
		int mount=Integer.parseInt(request.getParameter("mount"));

//		从购物车中移除该键值对	
		cartmap.remove(b, mount);
//		把更新后的购物车提供给session,这样重新进入购车页面看到的数据是最新的
		hs.setAttribute("cartmap", cartmap);
		
		response.sendRedirect("cartview");
		
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
随着人口老龄化和空巢化等社会问题的日益严峻,养老问题及以及养老方式的变革成为了当前社会的发展焦点,传统的养老模式以救助型和独立型为主,社会养老的服务质量与老年人的养老需求还存在一定的差距,人们生活水平的提高以及养老多元化需求的增加都需要通过创新和灵活开放的养老模式来应对未来的养老需求,结合目前我国养老模式及养老服务问题的内容的分析,互助养老模式作为一种新型的养老模式结合自主互助的集体养老理念,帮助老年人实现了满足个性需求的养老方案,互助养老模式让老年人具备了双重角色的同时也实现可持续的发展特色。目前我国老年人的占比以每年5%的速度在飞速增长,养老问题及养老服务的提供已经无法满足当前社会养老的切实需求,在养老服务质量和养老产品的变革过程中需要集合多元化的养老模式来满足更多老人的养老需求。 鉴于我国目前人口老龄化的现状以及迅速扩张的养老服务需求,现有的养老模式已经无法应对和满足社会发展的需求,快速增长的养老人员以及养老服务供给不足造成了紧张的社会关系,本文结合当前养老服务的发展需求,利用SSM框架以及JSP技术开发设计一款正对在线互助养老的系统,通过系统平台实现养老机构信息的传递及线上预约,搭建了起了用户、养老机构以及系统管理员的三方数据平台,借助网页端实现在线的养老互助信息查询、养老机构在线预约以及求助需求等功能,通过自养互养的养老模式来帮助老年人重新发现自我价值以及丰富养老的主观能动性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值