Servlet版購物車

2021-06-21 12:07:46 字數 1632 閱讀 9029

1.通過servlet來構造乙個最簡單的購物車,servlet是jsp的基礎,因此利用這個例子來看看servlet的一些要點。因此,這裡做一下限制,只能使用servlet和html。

html**如下:

pears

go to checkout

要點:

這部分用於控制禁止客戶端快取。

2.selectionservlet如下

selectionservlet用於判斷使用者選擇。並將快取放到session到中。使用的是session.setattribute()方法。

如果使用者行為為checkout,這直接重定向為 checkoutservlet,否則執行sendpage方法。該方法用於輸出乙個供使用者做進一步選擇的介面。

根據mvc的設計原則,servlet理應只作為控制器,但這裡用起做結果輸出,使**顯的非常的不可控。

private void sendpage(httpservletresponse response, string curprod) throws ioexception
這裡輸出了一張表單,並且該表單的action指定的是 weightservlet。

}}這裡再次進行flow control,根據使用者的選擇做出不同的響應。更為關鍵的是,每次做出的選擇我們都使用了session進行了跟蹤,將其儲存在伺服器端,當使用者再次選擇時,不會丟失結果。

與設定session相似的是,得到session的值使用 session.getattribute()方法。同樣,刪除session的方法時呼叫 session.removeattribute()方法。

doremove()和doadd()方法如下:

最後,我們關注最終**的checkoutservlet,其用於顯示使用者的購物列表。

}}這裡的關鍵是取得session中的多條記錄,使用session.getattributenames()方法。

不得不說,使用servlet做檢視的輸出非常的費勁。

購物車登陸 購物版

購物車 import os product list iphone7 5800 coffee 30 疙瘩湯 10 python book 99 bike 199 vivo x9 2499 shopping cart current userinfo db file r db.txt while tr...

簡陋版購物車 練習

goods msg 0 奧特曼 1 鋼鐵俠 2 笨方法學python 3 泰國一日遊 4 iphonex 5 娃娃 6 阿拉丁 7 特斯拉 goods dict username info list shopping car dict def username pwd inp username in...

購物車(註冊 登入 購物 購物車 結帳)

購物車 註冊 登入 購物 購物車 結帳 shopping car dict dict money 0 def input username pwd username input username pwd input pwd return username,pwd def goods get with...