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

利用session實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)功能

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

本文實(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

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

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

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

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

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

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

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

    java中使用@Transactional會(huì)有哪些坑

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

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

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

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

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

    eclipse老是自動(dòng)跳到console解決辦法

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

    Java中ReentrantLock4種常見(jiàn)的坑

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

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

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

    mybatis接收以逗號(hào)分隔的字符串批量查詢方式

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

最新評(píng)論

呼图壁县| 安庆市| 滁州市| 玛曲县| 崇文区| 延庆县| 贵德县| 江安县| 名山县| 年辖:市辖区| 沾益县| 左贡县| 江安县| 绥宁县| 沛县| 平遥县| 玉溪市| 集安市| 石泉县| 泰宁县| 土默特右旗| 富蕴县| 建瓯市| 广南县| 含山县| 美姑县| 延边| 绿春县| 呼伦贝尔市| 团风县| 河津市| 舞钢市| 万山特区| 深泽县| 鄢陵县| 牟定县| 文水县| 铜山县| 大余县| 会昌县| 扶沟县|