『壹』 Java中用session和hashmap實現最簡單的購物車,不需要bean,servlet或者資料庫。
session是httpservlet的一個類,你不用servlet,根本就沒有session這個玩意。簡單來說,session是跑在servlet上的汽車,沒有路你的車還能跑,怕不是變形金剛哦
『貳』 求助java+jsp+javaBean(一段關於session購物車程序)
錯誤在於你for循環抄裡面的setCount。
應該是ci.setCount(item.getCount() + ci.getCount());
不是item.setCount。
你的邏輯是,先查找items裡面是否有該商品,如果有,更新數量;如果沒有,向列表中添加一個新的item。所以for循環里是查找到的情況,對你查到的那條商品(ci),設置新的數量(setCount)即可。
而item是你傳過來的參數,item.setCount沒有意義。
『叄』 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>
『肆』 請問如何基於javaee用cookie實現購物車的代碼,謝謝了
.怎麼說呢 用cookie實現購物車根本就沒意義 .換太電腦就啥都沒了...
『伍』 session+cookies實現購物車功能,javaweb開發
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'shop2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
Map<String,Integer> itm=(Map<String,Integer>)session.getAttribute("item");
if(itm==null){
itm=new HashMap<String,Integer>();
itm.put("電視", 1);
itm.put("蘋果", 0);
itm.put("香蕉", 1);
itm.put("衣服", 0);
}
String [] buy=request.getParameterValues("c");
for(String b:buy){
if(b.equals("電視")){
int num=itm.get("電視");
itm.put(b, num+1);
}
if(b.equals("蘋果")){
int num=itm.get(b);
itm.put(b, num+1);
}
if(b.equals("香蕉")){
int num=itm.get(b);
itm.put(b, num+1);
}
if(b.equals("衣服")){
int num=itm.get(b);
itm.put(b, num+1);
}
}
session.setAttribute("item", itm);
%>
你所購買的物品<br>
電視:<%=itm.get("電視") %>本<br>
蘋果:<%=itm.get("蘋果") %>個<br>
香蕉:<%=itm.get("香蕉") %>個<br>
衣服:<%=itm.get("衣服") %>件<br>
<p><a href="shop.jsp">再次購買</a></p>
</body>
</html>
下面是購買頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'shop.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
歡迎光臨本店! <br>
<form action="shop2.jsp" method="post">
<input name="c" type="checkbox" value="電視" />電視
<input name="c" type="checkbox" value="蘋果" />蘋果
<input name="c" type="checkbox" value="香蕉" />香蕉
<input name="c" type="checkbox" value="衣服" />衣服
<img alt="dddd" src="<%=request.getAttribute("path")%>">
<p> <%=request.getAttribute("yy")%></p>
<input name="" type="submit" value="確認購買" />
</form>
</body>
</html>
下面是使用cookie的
添加Cookie
Cookie uname1=new Cookie("lname",username);
uname1.setMaxAge(24*3600);
Cookie upwd1=new Cookie("lpwd",pwd);
upwd1.setMaxAge(24*3600);
response.addCookie(uname1);
response.addCookie(upwd1);
添加session
session.setAttribute("log_name", username);
以下代碼是對cookie和session的數據操作!
<%
String uname="";
String upwd="";
Cookie[] cookies=request.getCookies(); //一request獲范圍獲取一個
cookie實例
if(cookies!=null){
for(Cookie c: cookies){
if("lname".equals(c.getName())){ //判斷cookie裡面的名字是否等於這
個
uname=c.getValue(); //如果等於就獲取它的值
}
if("lpwd".equals(c.getName())){
upwd=c.getValue();
}
}
String sql2="select * from member where username='"+uname+"'";
ResultSet rs2=null;
rs2=DBHelper.executeQuery(sql2);
boolean is=true;
while(rs2.next()){
String pp=rs2.getString("upwd");
if(pp.equals(upwd)){
is=false;
}
}
if(session.getAttribute("log_name")!=null){ //判斷是否有slog_name這
個session
uname=(String)session.getAttribute("log_name");
is=false;
}
if(is){
response.sendRedirect("index1.jsp");
}
}
%>
<%
application.setAttribute("log", uname);
%>
『陸』 關於spring mvc的session實現購物車問題
下單後把CART從session中移除啊
『柒』 在做一個java web項目使用session保持購物車信息。
session表示當前會話,也就是說瀏覽關閉之後它就沒有了,切換用戶也會沒有,最好保存在資料庫中
『捌』 購物車用session怎麼實現的
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
response.setContentType("text/html;charset=utf-8");
PrintWriterout=response.getWriter();
//獲得session
request.getSession();//創建了一個session
//獲得所有商品
Map<String,Book>all=DB.getAll();
out.write("本站提供如下商品<br>");
for(Entry<String,Book>e:all.entrySet()){
Bookbook=e.getValue();
//超鏈接要url重寫
//url重寫做了兩件事情,第一,獲得session的id,第二,將id加到地址後面
Stringurl="/javaweb07/servlet/BuyServlet?id="+book.getId();
StringnewUrl=response.encodeURL(url);
out.write(book.getName()+"<ahref='"+newUrl+"'target='_blank'>購買</a><br>");
}
}
BuyServlet.java
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
//獲得id
Stringid=request.getParameter("id");
//找出書
Bookbook=DB.find(id);
//獲得購物車
HttpSessionsession=request.getSession();
Listcart=(List)session.getAttribute("cart");
if(cart==null){
//剛進超市,分配購物車
cart=newArrayList();
}
//加入購物車
cart.add(book);
//將購物車找個地方存起來,方便下次購買
session.setAttribute("cart",cart);
//第一次創建session的時候需要發Cookie,將sessionid帶給瀏覽器
//獲得sessionid
Stringsessionid=session.getId();
//發送cookie,Cookie的key是區分大小寫
Cookiecookie=newCookie("JSESSIONID",sessionid);
//設置有效時間
cookie.setMaxAge(60*60*5);
response.addCookie(cookie);
//請求重定向到購物車頁面
Stringurl="/javaweb07/servlet/ListCartServlet";
//url重寫
StringnewUrl=response.encodeRedirectURL(url);
response.sendRedirect(newUrl);
}
ListCarServlet
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
response.setContentType("text/html;charset=utf-8");
PrintWriterout=response.getWriter();
//獲取購物車
//getSession方法會拿著用戶的jsessionid去內存找session找不到才創建
HttpSessionsession=request.getSession();
List<Book>cart=(List<Book>)session.getAttribute("cart");
if(cart==null){
out.write("對不起!您還沒有購買商品!!!");
return;
}
//遍歷購物車中的商品顯示
out.write("您購買了如下商品:<br>");
for(Bookbook:cart){
out.write(book.getName()+","+book.getPrice()+"<br>");
}
//讓session立即失效
//session.invalidate();
out.write("<inputtype='button'value='進入結算中心'/>");
}
『玖』 java web模擬購物車將list存放到session中,存在問題
因為從session獲取的那個是List集合,也就是說它返回的是堆內存的引用,版修改shopcar的東西權就是在修改堆內存的東西,所以你可以重新覆蓋session裡面的那個list,也可以選擇不覆蓋,效果是一樣的。但不建議選擇覆蓋因為可能會存在並發問題,即a用戶把b用戶的修改的值給覆蓋掉。
『拾』 java 購物車session的問題
頁面把商來品ID傳到後台,後台根據源ID查出商品,調用session.setAttribute(key,value)方法把商品信息放入session,可以把商品放到一個list里再放到session中 ,每添加一個商品就往改list里添加一個元素