response.addCookie(cookies);
<%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
??? pageEncoding="utf-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="dao.ItemsDao" %>
<%@ page import="entity.Items" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>詳情信息</title>
</head>
<body>
<h1>商品詳情</h1>
<hr>
<center>
<table width="750" height="60" cellpadding="0" cellspacing="0" border="0">
<tr>
<%
ItemsDao itemsdao = new ItemsDao();
Items items = itemsdao.getItemsById(Integer.parseInt(request.getParameter("id")));
if(items!= null)
{
%>
<td width="70%" valign="top">
<table>
?? ?<tr>
?? ??? ?<td rowspan="4"><img src="image/<%=items.getPicture() %>" width="200" height="160"></td>
?? ?</tr>
?? ?<tr>
?? ??? ?<td><%=items.getName() %></td>
?? ?</tr>
?? ?<tr>
?? ??? ?<td>產(chǎn)地:<%=items.getCity() %></td>
?? ?</tr>
?? ?<tr>
?? ??? ?<td>價格:¥<%=items.getPrice() %></td>
?? ?</tr>
</table>
</td>
<%
}
%>
<%
String list = "";
Cookie[] cookie = request.getCookies();
if(cookie != null && cookie.length>0){
?? ?for(Cookie c:cookie){
?? ??? ?if(c.getName().equals("ListCookie")){
?? ??? ??? ?list = c.getValue();
?? ??? ?}
?? ?}
}
?? ??? ?list += request.getParameter("id")+",";
?? ??? ?String[] str = list.split(",");
?? ??? ?if(str != null && str.length > 0){
?? ??? ??? ?if(str.length >= 1000){
?? ??? ??? ??? ?list = "";
?? ??? ??? ?}
?? ??? ?}
?? ??? ?Cookie cookies = new Cookie("ListCookie",list);
?? ??? ?response.addCookie(cookies);??? /// 這兒報錯了、、、、、、
%>
<td width="30%" char="#EEE" align="center">
<p>您瀏覽過的商品:</p>
?? ?<div>
?? ?<%
?? ?ArrayList<Items> itemslist = itemsdao.getCookieList(list);
?? ?if(itemslist != null && itemslist.size()>0){
?? ??? ?for(Items i:itemslist){
?? ?
?? ?%>
?? ??? ?<dl>
?? ??? ??? ?<dt>
?? ??? ??? ??? ?<a href="detail.jsp?id=<%=i.getId() %>"><img width="230" height="200" border="" src="image/<%=i.getPicture()%>"></a>
?? ??? ??? ?</dt>
?? ??? ??? ?<dd><%=i.getName() %></dd>
?? ??? ??? ?<dt>產(chǎn)地:<%=i.getCity() %> 價格:<%=i.getPrice() %></dt>
?? ??? ?</dl>
?? ?</div>
?? ?<%}
?? ??? ?}%>
</td>
</tr>
</table>
</center>
</body>
</html>
2020-12-07
牛逼的回答可以奧
2018-07-23
如果是用的eclipse開發(fā)工具,則需要解碼轉(zhuǎn)碼,然后導(dǎo)入java.net.*包就行了
2018-01-11
//1.保存list到cookie中時需要轉(zhuǎn)碼
Cookie cookie = new Cookie("ListViewCookie", URLEncoder.encode(list, "UTF-8"));
response.addCookie(cookie);
//2.讀取cookie,遍歷這個Cookies集合的時候需要解碼
if(cookies!=null && cookies.length>0) {
????for(Cookie c : cookies) {
????????if("ListViewCookie".equals(c.getName())) {
????????????//list = c.getValue();
????????????//由于cookie保存list的時候,經(jīng)過轉(zhuǎn)碼,所以提取的時候也要解碼
????????????list = URLDecoder.decode(c.getValue(), "UTF-8");
????????}
????}
}
前面有人已經(jīng)解答了。
2018-01-05
報這個錯?? 啥意思????