问答详情
源自:8-8 使用Cookie实现保存商品浏览记录

为何一直报错啊

<%@page import="java.net.URLDecoder"%>
<%@page import="java.net.URLEncoder"%>
<%@ page language="java" import="java.util.*"
 contentType="text/html; charset=UTF-8"%>
<%@ page import="com.imooc.DAO.*"%>
<%@ page import="com.imooc.model.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'details.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="styles2.css">


</head>

<body>
 <h1>商品详情</h1>
 <hr>
 <%
    ItemsDao item = new ItemsDao();
    int id=Integer.parseInt(request.getParameter("id"));
    Items items =item.getItemsBYid(id);
     if(items!=null){
     %>

 <div id="detail">
  <dl>
   <dt>
    <a href="details.jsp?id=<%=items.getId() %>"> <img alt=""
     src="images/<%=items.getPicture() %>"></a>
   </dt>
   <dd><%=items.getName() %>
   </dd>
   <dd>
    产地:<%=items.getCity() %>&nbsp;&nbsp;价格:<%=items.getPrice() %></dd>
  </dl>
 </div>
 <%} %>
 <%
   String list ="";
   Cookie[] cookie = request.getCookies();
   if(cookie!=null&&cookie.length>0)
              {
               for(Cookie c:cookie)
               {
                   if(c.getName().equals("listViewCookies"))
                   {
                      list = URLDecoder.decode(c.getValue(), "UTF-8");
                   }
               }
           }
   String[] s = list.split(",");
   if(s!=null&&s.length>0)
              {
                  if(s.length>=1000)
                  {
                      list="";
                  }
              }
  
    list+=request.getParameter("id")+",";
   Cookie cookie1 = new Cookie("listViewCookies",URLEncoder.encode(list, "UTF-8"));
   response.addCookie(cookie1);
    %>
 <h2>历史记录</h2>
 <%
                ArrayList<Items> itemlist = item.getViewList(list);
                if(itemlist!=null&&itemlist.size()>0)
                {
                 
                   for(Items i:itemlist)
                   {
                        
             %>
 <div>
  <dl>
   <dt>
    <a href="details.jsp?id=<%=i.getId()%>"><img
     src="images/<%=i.getPicture() %>" width="120" height="90"
     border="1" /></a>
   </dt>
   <dd><%=i.getName() %></dd>
   <dd>
    产地:<%=i.getCity() %>&nbsp;&nbsp;价格:<%=i.getPrice() %>
    ¥
   </dd>
  </dl>
 </div>
 <%
                   }
                }
             %>
</body>
</html>




HTTP Status [500] – [Internal Server Error]

Type Exception Report

Message An exception occurred processing JSP page [/details.jsp] at line [81]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: An exception occurred processing JSP page [/details.jsp] at line [81]

78:     %>
79: <h2>鍘嗗彶璁板綍</h2>
80: <%
81:                 ArrayList<Items> itemlist = item.getViewList(list);
82:                 if(itemlist!=null&&itemlist.size()>0)
83:                 {
84:                  


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:588)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Root Cause

java.lang.ArrayIndexOutOfBoundsException: 107
com.imooc.DAO.ItemsDao.getViewList(ItemsDao.java:119)
org.apache.jsp.details_jsp._jspService(details_jsp.java:219)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Note The full stack trace of the root cause is available in the server logs.


提问者:qq_逆时针_1 2017-05-24 22:27

个回答

  • noticeyou
    2018-04-30 23:25:51

    说的回答每一个是正确的,醉了

  • 慕函数4910142
    2017-06-15 16:07:32

    也可以将,分隔符换成/

  • 花南渊海
    2017-06-15 14:42:07

    我的和你的一样,你的是怎么解决的?求大佬相助

  • qq_逆时针_1
    2017-05-24 22:27:38

    错误是

    HTTP Status [500] – [Internal Server Error]

    Type Exception Report

    Message An exception occurred processing JSP page [/details.jsp] at line [70]

    Description The server encountered an unexpected condition that prevented it from fulfilling the request.

    Exception

    org.apache.jasper.JasperException: An exception occurred processing JSP page [/details.jsp] at line [70]

    67:               }
    68:    
    69:    Cookie cookie1 = new Cookie("listViewCookies",list);
    70:               response.addCookie(cookie1);
    71:     %>
    72:   <h2>鍘嗗彶璁板綍</h2>
    73:  <%


    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:588)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

    Root Cause

    java.lang.IllegalArgumentException: An invalid character [44] was present in the Cookie value
    org.apache.tomcat.util.http.Rfc6265CookieProcessor.validateCookieValue(Rfc6265CookieProcessor.java:182)
    org.apache.tomcat.util.http.Rfc6265CookieProcessor.generateHeader(Rfc6265CookieProcessor.java:115)
    org.apache.catalina.connector.Response.generateCookieString(Response.java:1009)
    org.apache.catalina.connector.Response.addCookie(Response.java:961)
    org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:383)
    org.apache.jsp.details_jsp._jspService(details_jsp.java:202)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

    Note The full stack trace of the root cause is available in the server logs.