利用session實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)功能
本文實(shí)例為大家分享了利用session實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)功能的具體代碼,供大家參考,具體內(nèi)容如下
一、實(shí)現(xiàn)的功能
(1) 利用session實(shí)現(xiàn)購(gòu)物車(chē)中的物品添加。
(2)使用servlet實(shí)現(xiàn)添加物品的功能(交互層)。
(3)一共有三個(gè)界面。第一個(gè)用來(lái)顯示物品的列表的頁(yè)面,第二個(gè)用來(lái)顯示添物品的界面,第三個(gè)用來(lái)顯示添加到購(gòu)物車(chē)的信息頁(yè)面。
二、代碼實(shí)現(xiàn)
(1)物品列表頁(yè)面: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)添加購(gòu)物車(chē)頁(yè)面: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)品的詳細(xì)參數(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 %>" >加入購(gòu)物車(chē)</a>
</body>
</html>(3)顯示添加成功后的信息頁(yè)面: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");
?? ??? ?
?? ??? ?//在這個(gè)方法里邊處理所有的增刪改查的功能
?? ??? ?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");
?? ??? ?//添加購(gòu)物車(chē)
?? ??? ?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");
?? ?}
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JAVAEE中用Session簡(jiǎn)單實(shí)現(xiàn)購(gòu)物車(chē)功能示例代碼
- java web開(kāi)發(fā)之實(shí)現(xiàn)購(gòu)物車(chē)功能
- java商城項(xiàng)目實(shí)戰(zhàn)之購(gòu)物車(chē)功能實(shí)現(xiàn)
- java web開(kāi)發(fā)之購(gòu)物車(chē)功能實(shí)現(xiàn)示例代碼
- javaweb圖書(shū)商城設(shè)計(jì)之購(gòu)物車(chē)模塊(3)
- java實(shí)現(xiàn)網(wǎng)上購(gòu)物車(chē)程序
- eclipse的web項(xiàng)目實(shí)現(xiàn)Javaweb購(gòu)物車(chē)的方法
- javaweb購(gòu)物車(chē)案列學(xué)習(xí)開(kāi)發(fā)
- JavaWeb后臺(tái)購(gòu)物車(chē)類實(shí)現(xiàn)代碼詳解
相關(guān)文章
Java從ftp服務(wù)器上傳與下載文件的實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于Java從ftp服務(wù)器上傳與下載文件的實(shí)現(xiàn)方法,最近項(xiàng)目中需要實(shí)現(xiàn)將文件先存放到ftp上,需要的時(shí)候再?gòu)膄tp上下載,做的過(guò)程中碰到了問(wèn)題,所以這里總結(jié)下,需要的朋友可以參考下2023-08-08
spring @Cacheable擴(kuò)展實(shí)現(xiàn)緩存自動(dòng)過(guò)期時(shí)間及自動(dòng)刷新功能
用過(guò)spring cache的朋友應(yīng)該會(huì)知道,Spring Cache默認(rèn)是不支持在@Cacheable上添加過(guò)期時(shí)間的,雖然可以通過(guò)配置緩存容器時(shí)統(tǒng)一指定,本文主要介紹了如何基于spring @Cacheable擴(kuò)展實(shí)現(xiàn)緩存自動(dòng)過(guò)期時(shí)間以及緩存即將到期自動(dòng)刷新,2024-02-02
解決"XML Parser Error on line 1: 前言中不允許有內(nèi)容"錯(cuò)誤
解決用windows自帶的記事編輯xml文件后出現(xiàn) "XML Parser Error on line 1: 前言中不允許有內(nèi)容。"的錯(cuò)誤2018-02-02
解決maven build 無(wú)反應(yīng),直接terminated的問(wèn)題
下面小編就為大家?guī)?lái)一篇解決maven build 無(wú)反應(yīng),直接terminated的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
Springboot如何通過(guò)yml配置文件為靜態(tài)成員變量賦值
這篇文章主要介紹了Springboot如何通過(guò)yml配置文件為靜態(tài)成員變量賦值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
SpringBoot整合MybatisPlus實(shí)現(xiàn)增刪改查功能
MybatisPlus是國(guó)產(chǎn)的第三方插件,?它封裝了許多常用的CURDapi,免去了我們寫(xiě)mapper.xml的重復(fù)勞動(dòng)。本文將整合MybatisPlus實(shí)現(xiàn)增刪改查功能,感興趣的可以了解一下2022-05-05
利用github搭建個(gè)人maven倉(cāng)庫(kù)的方法步驟

