利用session实现简单购物车功能

网友投稿 1462 2022-11-01

利用session实现简单购物车功能

利用session实现简单购物车功能

本文实例为大家分享了利用session实现简单购物车功能的具体代码,供大家参考,具体内容如下

一、实现的功能

(1) 利用session实现购物车中的物品添加。(2)使用servlet实现添加物品的功能(交互层)。(3)一共有三个界面。第一个用来显示物品的列表的页面,第二个用来显示添物品的界面,第三个用来显示添加到购物车的信息页面。

二、代码实现

(1)物品列表页面:productlist.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%>

联想拯救者R7



联想拯救者R8



联想拯救者R9



联想拯救者R10



(2)添加购物车页面:producttails.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%>

&lttROIkcwx;title>Insert title here

<%

String pname = (String)request.getAttribute("p");

out.println(pname);

%>



拿到其他的......该产品的详细参数



加入购物车

(3)显示添加成功后的信息页面:shoppingcart.jsp

<%@page import="java.util.List"%>

<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%>

<%

List products = (List)session.getAttribute("car");

for(String s: products){

out.print(s+"

");

}

%>

(4)交互层:ShopController.java

package com.controller;

import java.io.IOException;

import java.lang.reflect.Method;

import java.util.ArrayList;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

/**

* Servlet implementation class ShopController

*/

@WebServlet(urlPatterns = {"*.pdo"})

pubhttp://lic class ShopController extends HttpServlet {

private static final long serialVersionUID = 1L;

/**

* @see HttpServlet#HttpServlet()

*/

public ShopController() {

super();

// TODO Auto-generated constructor stub

}

/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

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

//设置字符集

request.setCharacterEncoding("utf-8");

response.setCharacterEncoding("utf-8");

response.setCharacterEncoding("text/html; charset=utf-8");

//在这个方法里边处理所有的增删改查的功能

String mn = request.getServletPath();

mn = mn.substring(1);

mn = mn.substring(0 , mn.length()-4);

//利用反射

try {

//获取方法名,并且根据具体的去调用下边的方法

Method method = this.getClass().getDeclaredMethod(mn,HttpServletRequest.class , HttpServletResponse.class);

method.invoke(this,request,response);

} catch (NoSuchMethodException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

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

// TODO Auto-generated method stub

doGet(request, response);

}

private void shopping (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//设置字符集

request.setCharacterEncoding("utf-8");

response.setCharacterEncoding("utf-8");

response.setCharacterEncoding("text/html; charset=utf-8");

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

request.setAttribute("p", pname);

request.getRequestDispatcher("/producttails.jsp").forward(request, response);;

}

private void addcart (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//设置字符集

request.setCharacterEncoding("utf-8");

response.setCharacterEncoding("utf-8");

response.setCharacterEncoding("text/html; charset=utf-8");

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

//添加购物车

HttpSession session = request.getSession(true);

List products = (List)session.getAttribute("car");

if(products == null) {

products = new ArrayList<>();

}

//把添加的物品放入List集合

products.add(pname);

//放入session中表示添加成功

session.setAttribute("car", products);

//response.getWriter().print("添加成功!");

response.sendRedirect(request.getContextPath() + "/shoppingcart.jsp");

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:shell脚本和命令行应用程序的实时、原生微型图形用户界面
下一篇:Spring Cloud源码分析之Eureka第六章:服务注册
相关文章

 发表评论

暂时没有评论,来抢沙发吧~