欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2022年02月09日 09:56:14   作者:一只小小的螞蟻  
這篇文章主要為大家詳細介紹了利用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)文章

  • 利用github搭建個人maven倉庫的方法步驟

    利用github搭建個人maven倉庫的方法步驟

    這篇文章主要介紹了利用github搭建個人maven倉庫的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • 最新評論