JAVAEE中用Session簡(jiǎn)單實(shí)現(xiàn)購(gòu)物車(chē)功能示例代碼
Session簡(jiǎn)單實(shí)現(xiàn)購(gòu)物車(chē)功能
這個(gè)小程序主要就3個(gè)頁(yè)面,一個(gè)商品列表頁(yè)面(HomeServlet),一個(gè)是提示加入購(gòu)物車(chē)頁(yè)面(AddCartTipServlet),一個(gè)是顯示購(gòu)物車(chē)清單頁(yè)面(ShowCartServlet)。
HomeServlet頁(yè)面:
@WebServlet({ "/HomeServlet", "/home" }) public class HomeServlet extends HttpServlet { private static final long serialVersionUID = 1L; public HomeServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.print("<h2>書(shū)單</h2><hr/><br/>"); out.print("人類(lèi)簡(jiǎn)史<a href='"+request.getContextPath()+"/addCartTip?id=1'>加入購(gòu)物車(chē)</a><br/>"); out.print("未來(lái)簡(jiǎn)史<a href='"+request.getContextPath()+"/addCartTip?id=2'>加入購(gòu)物車(chē)</a><br/>"); out.print("世界簡(jiǎn)史<a href='"+request.getContextPath()+"/addCartTip?id=3'>加入購(gòu)物車(chē)</a><br/>"); out.print("時(shí)間簡(jiǎn)史<a href='"+request.getContextPath()+"/addCartTip?id=4'>加入購(gòu)物車(chē)</a><br/>"); out.print("<a href='"+request.getContextPath()+"/show/cart'>查看購(gòu)物車(chē)</a><br/>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
AddCartTipServlet頁(yè)面:
@WebServlet({ "/AddCartTipsServlet", "/addCartTip" }) public class AddCartTipsServlet extends HttpServlet { private static final long serialVersionUID = 1L; public AddCartTipsServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); HttpSession session = request.getSession(); List<String> list = (List<String>) session.getAttribute("cart"); if(list==null){ list=new ArrayList<>(); } String id = request.getParameter("id"); list.add(id); session.setAttribute("cart", list); System.out.println(list.toString()); response.getWriter().println("已加入購(gòu)物車(chē)<br/>" + "<a href='"+request.getContextPath()+"/home'>繼續(xù)購(gòu)物</a><br/>" + "<a href='"+request.getContextPath()+"/show/cart'>查看購(gòu)物車(chē)</a><br/>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
ShowCartSevlet頁(yè)面
@WebServlet({ "/ShowCartServlet", "/show/cart" }) public class ShowCartServlet extends HttpServlet { private static final long serialVersionUID = 1L; public ShowCartServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); List<String> list = (List<String>)request.getSession().getAttribute("cart"); if(list!=null){ out.print("你的購(gòu)物清單:<br/>"); for (String string : list) { out.println(DBUtils.findById(string)+"<br/>"); } out.println("<br/><a href='"+request.getContextPath()+"/home'>繼續(xù)購(gòu)物</a><br/>"); }else{ out.println("你還沒(méi)有將商品添加到購(gòu)物車(chē)<br/>" + "<a href='"+request.getContextPath()+"/home'>返回商品列表</a><br/>"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
DBUtils:存儲(chǔ)著商品信息
public class DBUtils { private static Map<String,String> map = new HashMap<>(); static{ map.put("1", "人類(lèi)簡(jiǎn)史"); map.put("2", "未來(lái)簡(jiǎn)史"); map.put("3", "世界簡(jiǎn)史"); map.put("4", "時(shí)間簡(jiǎn)史"); } public static String findById(String id){ return map.get(id); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaWeb購(gòu)物車(chē)項(xiàng)目開(kāi)發(fā)實(shí)戰(zhàn)指南
- JavaWeb后臺(tái)購(gòu)物車(chē)類(lèi)實(shí)現(xiàn)代碼詳解
- eclipse的web項(xiàng)目實(shí)現(xiàn)Javaweb購(gòu)物車(chē)的方法
- javaweb購(gòu)物車(chē)案列學(xué)習(xí)開(kāi)發(fā)
- java web開(kāi)發(fā)之購(gòu)物車(chē)功能實(shí)現(xiàn)示例代碼
- javaweb圖書(shū)商城設(shè)計(jì)之購(gòu)物車(chē)模塊(3)
- java web開(kāi)發(fā)之實(shí)現(xiàn)購(gòu)物車(chē)功能
- java商城項(xiàng)目實(shí)戰(zhàn)之購(gòu)物車(chē)功能實(shí)現(xiàn)
- java實(shí)現(xiàn)網(wǎng)上購(gòu)物車(chē)程序
- Java?web實(shí)現(xiàn)購(gòu)物車(chē)案例
相關(guān)文章
Java數(shù)據(jù)類(lèi)型Integer與int的區(qū)別詳細(xì)解析
這篇文章主要介紹了Java數(shù)據(jù)類(lèi)型Integer與int的區(qū)別詳細(xì)解析,Ingeter是int的包裝類(lèi),int的初值為0,Ingeter的初值為null,int和integer(無(wú)論new否)比,都為true,因?yàn)闀?huì)把Integer自動(dòng)拆箱為int再去比,需要的朋友可以參考下2023-12-12加速spring/springboot應(yīng)用啟動(dòng)速度詳解
這篇文章主要介紹了加速spring/springboot應(yīng)用啟動(dòng)速度詳解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì)對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07Java自定義equals產(chǎn)生的問(wèn)題分析
這篇文章主要介紹了Java自定義equals時(shí)super.equals帶來(lái)的問(wèn)題分析,總的來(lái)說(shuō)這并不是一道難題,那為什么要拿出這道題介紹?拿出這道題真正想要傳達(dá)的是解題的思路,以及不斷優(yōu)化探尋最優(yōu)解的過(guò)程。希望通過(guò)這道題能給你帶來(lái)一種解題優(yōu)化的思路2023-01-01Java采用setAsciiStream方法檢索數(shù)據(jù)庫(kù)指定內(nèi)容實(shí)例解析
這篇文章主要介紹了Java采用setAsciiStream方法檢索數(shù)據(jù)庫(kù)指定內(nèi)容,是比較實(shí)用的功能,需要的朋友可以參考下2014-08-08Spring Security學(xué)習(xí)筆記(一)
這篇文章主要介紹了Spring Security的相關(guān)資料,幫助大家開(kāi)始學(xué)習(xí)Spring Security框架,感興趣的朋友可以了解下2020-09-09