最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用session實現(xiàn)簡單購物車功能

 更新時間:2022年02月09日 09:56:14   作者:一只小小的螞蟻  
這篇文章主要為大家詳細(xì)介紹了利用session實現(xiàn)簡單購物車功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了利用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)品的詳細(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 %>" >加入購物車</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)文章

  • mybatis利用association或collection傳遞多參數(shù)子查詢

    mybatis利用association或collection傳遞多參數(shù)子查詢

    今天小編就為大家分享一篇關(guān)于mybatis利用association或collection傳遞多參數(shù)子查詢,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Java數(shù)據(jù)庫連接池的幾種配置方法(以MySQL數(shù)據(jù)庫為例)

    Java數(shù)據(jù)庫連接池的幾種配置方法(以MySQL數(shù)據(jù)庫為例)

    這篇文章主要介紹了Java數(shù)據(jù)庫連接池的幾種配置方法(以MySQL數(shù)據(jù)庫為例) 的相關(guān)資料,需要的朋友可以參考下
    2016-07-07
  • Java中的信號量Semaphore詳細(xì)解讀

    Java中的信號量Semaphore詳細(xì)解讀

    這篇文章主要介紹了Java中的信號量Semaphore詳細(xì)解讀,Java信號量機(jī)制可以用來保證線程互斥,創(chuàng)建Semaphore對象傳入一個整形參數(shù),類似于公共資源,需要的朋友可以參考下
    2023-11-11
  • java中使用@Transactional會有哪些坑

    java中使用@Transactional會有哪些坑

    在Java中,@Transactional是一個常用的注解,用于聲明方法應(yīng)該在一個事務(wù)的上下文中執(zhí)行,本文主要介紹了java中使用@Transactional會有哪些坑,感興趣的可以了解一下
    2024-04-04
  • JavaWeb學(xué)習(xí)筆記分享(必看篇)

    JavaWeb學(xué)習(xí)筆記分享(必看篇)

    下面小編就為大家?guī)硪黄狫avaWeb學(xué)習(xí)筆記分享(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • SpringMVC異常處理的三種方式小結(jié)

    SpringMVC異常處理的三種方式小結(jié)

    本文主要介紹了SpringMVC異常處理的三種方式小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-09-09
  • eclipse老是自動跳到console解決辦法

    eclipse老是自動跳到console解決辦法

    eclipse啟動服務(wù)后,想看一些properties信息或者別的,但老是自動跳轉(zhuǎn)到console頁面,本文給大家介紹了解決辦法,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-03-03
  • Java中ReentrantLock4種常見的坑

    Java中ReentrantLock4種常見的坑

    本文主要介紹了Java中ReentrantLock?4種常見的坑,ReentrantLock默認(rèn)情況下為非公平鎖,下文關(guān)于其更多詳情需要的小伙伴可以參考一下
    2022-05-05
  • Java不定參數(shù)使用及一些注意情況

    Java不定參數(shù)使用及一些注意情況

    不定參數(shù)是一種特殊的參數(shù)類型,它允許方法接受可變數(shù)量的參數(shù),本文主要介紹了Java不定參數(shù)使用及一些注意情況,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • mybatis接收以逗號分隔的字符串批量查詢方式

    mybatis接收以逗號分隔的字符串批量查詢方式

    這篇文章主要介紹了mybatis接收以逗號分隔的字符串批量查詢方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01

最新評論

图木舒克市| 勐海县| 广宁县| 崇文区| 洛南县| 青岛市| 蓬莱市| 亳州市| 英超| 河西区| 古田县| 乌鲁木齐市| 大渡口区| 蓬莱市| 宁南县| 佛山市| 社旗县| 黄骅市| 治多县| 新郑市| 泰宁县| 龙岩市| 讷河市| 牙克石市| 怀远县| 石景山区| 南雄市| 镇沅| 霍州市| 沙洋县| 麻城市| 长乐市| 佳木斯市| 延安市| 清苑县| 成武县| 桂东县| 襄城县| 新津县| 郑州市| 德格县|