利用session實現(xiàn)簡單購物車功能
本文實例為大家分享了利用session實現(xiàn)簡單購物車功能的具體代碼,供大家參考,具體內(nèi)容如下
一、實現(xiàn)的功能
(1) 利用session實現(xiàn)購物車中的物品添加。
(2)使用servlet實現(xiàn)添加物品的功能(交互層)。
(3)一共有三個界面。第一個用來顯示物品的列表的頁面,第二個用來顯示添物品的界面,第三個用來顯示添加到購物車的信息頁面。
二、代碼實現(xiàn)
(1)物品列表頁面:productlist.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" ? ? pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> ?? ?<a href="<%=request.getContextPath()%>/shopping.pdo?pname='LENOVO R7'">聯(lián)想拯救者R7</a> ?? ?<br><br> ?? ?<a href="<%=request.getContextPath()%>/shopping.pdo?pname='LENOVO R8'">聯(lián)想拯救者R8</a> ?? ?<br><br> ?? ?<a href="<%=request.getContextPath()%>/shopping.pdo?pname='LENOVO R9'">聯(lián)想拯救者R9</a> ?? ?<br><br> ?? ?<a href="<%=request.getContextPath()%>/shopping.pdo?pname='LENOVO R10'">聯(lián)想拯救者R10</a> ?? ?<br><br> </body> </html>
(2)添加購物車頁面:producttails.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" ? ? pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> ?? ??? ?<% ?? ??? ??? ?String pname = (String)request.getAttribute("p"); ?? ??? ??? ?out.println(pname); ?? ??? ?%> ?? ??? ?<br><br> ?? ??? ?拿到其他的......該產(chǎn)品的詳細參數(shù) ?? ??? ? ?? ??? ?<br><br> ?? ??? ?<a style="display: block;width: 100px ; height: 35px ;line-height :35px; text-decoration : none;background: red; color:#fff ;text-align: center;" href="<%=request.getContextPath() %>/addcart.pdo?pname=<%=pname %>" >加入購物車</a> </body> </html>
(3)顯示添加成功后的信息頁面:shoppingcart.jsp
<%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=utf-8" ? ? pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> ?? ??? ?<% ?? ??? ??? ?List<String> products = (List<String>)session.getAttribute("car"); ?? ??? ??? ?for(String s: products){ ?? ??? ??? ??? ?out.print(s+"<br><br>"); ?? ??? ??? ?} ?? ??? ?%> </body> </html>
(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"}) public 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 { ?? ??? ?//設(shè)置字符集 ?? ??? ?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 { ?? ??? ??? ?//獲取方法名,并且根據(jù)具體的去調(diào)用下邊的方法 ?? ??? ??? ?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 { ?? ??? ?//設(shè)置字符集 ?? ??? ?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 { ?? ??? ?//設(shè)置字符集 ?? ??? ?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<String> products = (List<String>)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"); ?? ?} }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java從ftp服務(wù)器上傳與下載文件的實現(xiàn)
這篇文章主要給大家介紹了關(guān)于Java從ftp服務(wù)器上傳與下載文件的實現(xiàn)方法,最近項目中需要實現(xiàn)將文件先存放到ftp上,需要的時候再從ftp上下載,做的過程中碰到了問題,所以這里總結(jié)下,需要的朋友可以參考下2023-08-08spring @Cacheable擴展實現(xiàn)緩存自動過期時間及自動刷新功能
用過spring cache的朋友應(yīng)該會知道,Spring Cache默認是不支持在@Cacheable上添加過期時間的,雖然可以通過配置緩存容器時統(tǒng)一指定,本文主要介紹了如何基于spring @Cacheable擴展實現(xiàn)緩存自動過期時間以及緩存即將到期自動刷新,2024-02-02解決"XML Parser Error on line 1: 前言中不允許有內(nèi)容"錯誤
解決用windows自帶的記事編輯xml文件后出現(xiàn) "XML Parser Error on line 1: 前言中不允許有內(nèi)容。"的錯誤2018-02-02解決maven build 無反應(yīng),直接terminated的問題
下面小編就為大家?guī)硪黄鉀Qmaven build 無反應(yīng),直接terminated的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06Springboot如何通過yml配置文件為靜態(tài)成員變量賦值
這篇文章主要介紹了Springboot如何通過yml配置文件為靜態(tài)成員變量賦值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10SpringBoot整合MybatisPlus實現(xiàn)增刪改查功能
MybatisPlus是國產(chǎn)的第三方插件,?它封裝了許多常用的CURDapi,免去了我們寫mapper.xml的重復(fù)勞動。本文將整合MybatisPlus實現(xiàn)增刪改查功能,感興趣的可以了解一下2022-05-05