sinat_35550357 2017-04-08 13:07 采纳率: 0%
浏览 890

利用cookie显示历史浏览记录 运行时抛角标越界异常

public class CookieDemo2 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    //解决乱码问题
    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
    PrintWriter out = response.getWriter();

    //输出网站所有商品
    Map<String, Book> map = DB.getAll();
    for(Map.Entry<String, Book> entry : map.entrySet()){
        Book book = entry.getValue();
        //System.out.println(book.getId());
        out.print("<a href='/servlet/CookieDemo3?id="+book.getId()+"' target='_blank'>"+book.getName()+"</a><br/>");  
    }

    //显示用户看过的商品
    out.write("你曾经看过如下商品<br>");
    Cookie[] cookies = request.getCookies();
    for(int x = 0;cookies != null && x<=cookies.length;x++){
        if(cookies[x].getName().equals("bookHistory")){
            String []ids = cookies[x].getValue().split("\\,");
            for(String s:ids){
                Book book = DB.getAll().get(s);
                out.write(book.getName()+"<br>");
            }
        }
    }

}


public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    doGet(request, response);
}

}

class DB{

public static Map<String, Book> map = new LinkedHashMap<String,Book>();
static{
    map.put("1", new Book("1","Java","王","一本好书"));
    map.put("2", new Book("2","HTML","刘","一本好书"));
    map.put("3", new Book("3","CSS","张","一本好书"));
    map.put("4", new Book("4","c++","杨","一本好书"));
}



public static Map<String, Book> getAll(){

    return map;
}

}

class Book{
private String id;
private String name;
private String author;
private String description;

public Book(String id, String name, String author, String description) {
    super();
    this.id = id;
    this.name = name;
    this.author = author;
    this.description = description;
}
public Book() {
    // TODO Auto-generated constructor stub
}
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getAuthor() {
    return author;
}
public void setAuthor(String author) {
    this.author = author;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}

}

public class CookieDemo3 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    //解决乱码问题
    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
    PrintWriter out = response.getWriter();

    //1.根据用户提交的id显示对应的商品的详细的信息  


    String id = request.getParameter("id");

    Book book = DB.getAll().get(id);
    out.write("id为"+book.getId()+"<br>");
    out.write("书名"+book.getName()+"<br>");
    out.write("作者"+book.getAuthor()+"<br>");
    out.write("描述"+book.getDescription()+"<br>");

    //2.构建Cookie,回写给浏览器  

    String cookieValue = buildCookie(id,request);
    Cookie cookie = new Cookie("bookHistory",cookieValue);  
    cookie.setMaxAge(30*24*60*60);
    cookie.setPath("/servlet");
    response.addCookie(cookie);


}


private String buildCookie(String id, HttpServletRequest request) {
    String bookHistory = null;
    Cookie[] cookies = request.getCookies();
    for(int x = 0;cookies!=null && x<=cookies.length;x++){
        if(cookies[x].getName().equals("bookHistory")){
        bookHistory = cookies[x].getValue();
        }
    }

    if(bookHistory==null){
        return id;
    }

    LinkedList<String> list = new LinkedList<String>(Arrays.asList(bookHistory.split("\\,")));
    if(list.contains(id)){
        list.remove(id);
    }else{
        if(list.size()>=3){
            list.removeLast();
        }
    }
    list.addFirst(id);

    StringBuffer sb = new StringBuffer();
    for(String ids : list){
        sb.append(ids+",");
    }

    return sb.deleteCharAt(sb.length()-1).toString();
}


public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    doGet(request, response);
}

}

这是两段代码,运行点击书名后显示demo3的详细信息,然后刷新demo2就抛java.lang.ArrayIndexOutOfBoundsException: 1 越界异常是怎么回事,完全看不出问题神,求指教

  • 写回答

1条回答 默认 最新

  • charm_of_code 2017-04-08 13:55
    关注

    在demo2里的 out.write("你曾经看过如下商品
    ");
    Cookie[] cookies = request.getCookies();
    for(int x = 0;cookies != null && x<=cookies.length;x++){
    if(cookies[x].getName().equals("bookHistory")){
    String []ids = cookies[x].getValue().split("\,");
    for(String s:ids){
    Book book = DB.getAll().get(s);
    out.write(book.getName()+"
    ");
    }
    }

                应该是x<cookies.length
    
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?