❶ 關於jsp中session購物車的使用
代碼1:null != goodsName
代碼2:list.add(goodsName);
session.setAttribute("list",list);
--------
if(list==null){
list=new ArrayList();
list.add(goodsName);
session.setAttribute("list",list);
}else{
此處填代碼2
}
這處優化一點可以這樣版
ArrayList list = (ArrayList)session.getAttribute("list");
if(list == null){
list = new ArrayList();
}
list.add(goodsName);
session.setAttribute("list",list);
。。權。。
❷ jsp用session做一個簡單的購物車
在 Session 存放一個 Map<int,int> ,Key是商品ID,Value是數量。
❸ jsp session購物車 刪除商品後 總價不變
HttpSession session = request.getSession ();
session.setMaxInactiveInterval (1000);
session.setAttribute ("", "");
❹ jsp購物車用session實現 100分高分求助
1:提交按鈕和購物車按鈕用不同的action,若非要用相同的action的話,那麼後面請添加不同的Paragram。在內ACTION裡面做判斷。容 一般一個action對應一個Form。寫不寫actionForm都無所謂。
2:最好用Map。 Map可以查找出來一個key的set,調用iterater循環,就可以得到所有的key。建議 key放產品,然後value放數量。
❺ jsp 中session的購物車問題
display.jsp 幫你修改了一下,有個小錯誤,見注釋:
<%@page language="java" contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>購物車</title>
</head>
<body>
<center>
您選購的商品
</center>
<br>
<hr>
<%
Integer itemCount = (Integer) session.getAttribute("itemCount");
if (itemCount == null) {
itemCount = new Integer(0);
}
%>
<%
String[] item = { "籃球", "足球", "乒乓球", "地球", "電腦", "電視機", "寶馬",
"法拉利", };
String[] itemsSelected = request.getParameterValues("item");
String itemName = "";
if (itemsSelected != null) {
for (int i = 0; i < itemsSelected.length; i++) {
itemName = itemsSelected[i];
session.setAttribute("item" + itemCount, itemName);
session.setAttribute("itemCount", itemCount);
//itemCount++這句應該放在這里,不然session.getAttribute("item0")==null
//你對比一下原來的代碼就知道為什麼getAttribute("item0")是null了
itemCount = new Integer(itemCount.intValue() + 1);
}
}
%>
<%
for (int i = 0; i < itemCount.intValue(); i++) {
String items = (String) session.getAttribute("item" + i);
int ite = 0;
out.println(items + " ");
ite = Integer.parseInt(items);
out.println(item[ite] + "<br>");
}
%>
</body>
</html>
❻ jsp實現購物車,請問用操作資料庫實現好,還是session做好
用資料庫存你得抄給資料庫造成多大的負擔啊, 而且對於購物車, 這種需要實時操作的東西, 資料庫的訪問量一大了, 就容易出現並發錯誤, 或者直接崩潰.
不可否認用Session確實效率很高, 而且會話是針對各個連接的, 所以便於管理, 但是用Session也不是完美的, 因為Session是有有效期的, 根據伺服器的設置不同而不一樣長, 如果你在購物的過程中Session超時了, 那麼購物車中的東西就會全沒了.
不知道你看過當當網的購物車沒有, 當你下線之後, 再次上線, 購物車中的東西還是存在的, 這對於用戶來說非常方便.
所以如果你的伺服器夠強的話, 你完全可以用一個靜態變數來保存所有用戶的購物車, 比如用一個靜態的Map, 以IP作為Key,區分不同用戶的購物車, 這樣就可以使用戶在下線的情況下也可以保存購物車中的內容.
這種方法實現過, 只是沒有用大量的並發訪問測試其穩定性, 但是一定是可行的.
❼ jsp如何清除購物車的session數據
session.remove("name");
❽ jsp session購物車類型
首先,我們可以看到,程序中首先是獲得
HttpSession s = request.getSession();
因為我們要把用戶已經選擇了的商品信息放到session裡面,至於為什麼放到
session裡面,樓主可以看看關於session 的講解內容,這里理解成session就是從用戶進到網站來到退出,一直存在的東西,就像超市裡的手推車一樣
好,言歸正傳
我們在獲得了session後,判斷session中是否放入了商品信息
ArrayList al = (ArrayList)s.getAttribute("gouwuche");
if(al == null)//第一次訪問,還沒有購物車
像代碼提示中所說,第一次應該是沒有
那麼執行if中的內容
ArrayList aaa = new ArrayList();
aaa.add(item);//item貨物信息
s.setAttribute("gouwuche",aaa);
之所以new 一個 ArrayList是因為我們使用ArrayList來存放商品信息
因為用戶不可能只買一個商品,所以需要用一個list去存放他們
這樣在選擇了第一件商品後,我們把它存放在list裡面,讓後把這個list放到session裡面
當第二次進入方法的時候
還是先判斷session裡面是否有東西
因為之前放過了
所以這回有了
那麼執行else裡面的內容else
{
al.add(item);
}
直接進行添加
這樣就實現了購物車的功能
另外樓主問道關於al的問題,也就ArrayList的問題
首先我們看到
ArrayList al = (ArrayList)s.getAttribute("gouwuche");
這一句類似於
獲得ArrayList的對象
但是沒有的情況下
(ArrayList)s.getAttribute("gouwuche");
就是null
那麼
ArrayList al = (ArrayList)s.getAttribute("gouwuche");
類似於
ArrayList al = null;
所以
if(al == null)
的時候我們要去new一個
感覺挺詳細的了
希望樓主能夠看明白
❾ jsp 如何在用session做完購物車後生成訂單,插入資料庫中
購物車是保存在session中的一個對象,當JSP提交的時候從session中得到購物車這個對象,然後持久化到資料庫中就可以了
❿ jsp 怎麼把存在session裡面的購物車訂單信息存到資料庫
<s:iterator value="dinnerServiceOuts1" id="dinner1" status="stat">
<s:hidden name="%{'dinnerServiceOuts1['+#stat.index+'].activeMode'}" value="%{dinnerServiceOuts1[#stat.index].activeMode}"/>
<s:hidden name="%{'dinnerServiceOuts1['+#stat.index+'].proctID'}" value="%{dinnerServiceOuts1[#stat.index].proctID}"/>
<s:hidden name="%{'dinnerServiceOuts1['+#stat.index+'].proctName'}" value="%{dinnerServiceOuts1[#stat.index].proctName}"/>
</s:iterator>
這樣寫才成否則你只能得到最後一個值