练涛

details.jsp页面的 response.addCookie(cookie);报错&tomcat高版本下的Cookie问题

测试Cookie项目显示浏览过商品的的信息时出现的问题

报错信息:
HTTP Status 500 - Unable to compile class for JSP

104: }
105: }
106: Cookie cookie = new Cookie(“ListViewCookie”,list);
107: response.addCookie(cookie); //这一行报错
108:
109: %>
110:

具体改detials.jsp中的,将+“,”改为加”#”;

list+=request.getParameter(“id”)+”#”;

          //如果浏览记录超过1000条,清零.

          String[] arr = list.split("#");

改ItemsDao.java中的

String[] arr = list.split(“#”);

detials.jsp代码:

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import util.DBHelper;

import entity.Items;

//商品的业务逻辑类
public class ItemsDAO {

    private static final char[] Items = null;
    // 获得所有的商品信息
    public ArrayList<Items> getAllItems() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        ArrayList<Items> list = new ArrayList<Items>(); // 商品集合
        try {
            conn = DBHelper.getConnection();
            String sql = "select * from items;"; // SQL语句
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            while (rs.next()) {
                Items item = new Items();
                item.setId(rs.getInt("id"));
                item.setName(rs.getString("name"));
                item.setCity(rs.getString("city"));
                item.setNumber(rs.getInt("number"));
                item.setPrice(rs.getInt("price"));
                item.setPicture(rs.getString("picture"));
                list.add(item);// 把一个商品加入集合
            }
            return list; // 返回集合。
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        } finally {
            // 释放数据集对象
            if (rs != null) {
                try {
                    rs.close();
                    rs = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            // 释放语句对象
            if (stmt != null) {
                try {
                    stmt.close();
                    stmt = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }

    }

    // 根据商品编号获得商品资料
    public Items getItemsById(int id) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = DBHelper.getConnection();
            String sql = "select * from items where id=?;"; // SQL语句
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, id);
            rs = stmt.executeQuery();
            if (rs.next()) {
                Items item = new Items();
                item.setId(rs.getInt("id"));
                item.setName(rs.getString("name"));
                item.setCity(rs.getString("city"));
                item.setNumber(rs.getInt("number"));
                item.setPrice(rs.getInt("price"));
                item.setPicture(rs.getString("picture"));
                return item;
            } else {
                return null;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        } finally {
            // 释放数据集对象
            if (rs != null) {
                try {
                    rs.close();
                    rs = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            // 释放语句对象
            if (stmt != null) {
                try {
                    stmt.close();
                    stmt = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

        }
    }
    //获取最近浏览的前五条商品信息
    public ArrayList<Items> getViewList(String list)
    {
        //System.out.println("商品ID:list:"+list);
        ArrayList<Items> itemlist = new ArrayList<Items>();
        int iCount=5; //每次返回前五条记录
        if(list!=null&&list.length()>0)
        {
            String[] arr = list.split("#");
          //  System.out.println("arr.length="+arr.length);
            //如果商品记录大于等于5条
            if(arr.length>=5)
            {
               for(int i=arr.length-1;i>=arr.length-iCount;i--)
               {
                  itemlist.add(getItemsById(Integer.parseInt(arr[i])));  
               }
            }
            else
            {
                for(int i=arr.length-1;i>=0;i--)
                {
                    itemlist.add(getItemsById(Integer.parseInt(arr[i])));
                }
            }
            return itemlist;
        }
        else
        {
            return null;
        }

    }

}

ItemsDao.java代码:

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import util.DBHelper;

import entity.Items;

//商品的业务逻辑类
public class ItemsDAO {

    private static final char[] Items = null;
    // 获得所有的商品信息
    public ArrayList<Items> getAllItems() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        ArrayList<Items> list = new ArrayList<Items>(); // 商品集合
        try {
            conn = DBHelper.getConnection();
            String sql = "select * from items;"; // SQL语句
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            while (rs.next()) {
                Items item = new Items();
                item.setId(rs.getInt("id"));
                item.setName(rs.getString("name"));
                item.setCity(rs.getString("city"));
                item.setNumber(rs.getInt("number"));
                item.setPrice(rs.getInt("price"));
                item.setPicture(rs.getString("picture"));
                list.add(item);// 把一个商品加入集合
            }
            return list; // 返回集合。
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        } finally {
            // 释放数据集对象
            if (rs != null) {
                try {
                    rs.close();
                    rs = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            // 释放语句对象
            if (stmt != null) {
                try {
                    stmt.close();
                    stmt = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }

    }

    // 根据商品编号获得商品资料
    public Items getItemsById(int id) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = DBHelper.getConnection();
            String sql = "select * from items where id=?;"; // SQL语句
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, id);
            rs = stmt.executeQuery();
            if (rs.next()) {
                Items item = new Items();
                item.setId(rs.getInt("id"));
                item.setName(rs.getString("name"));
                item.setCity(rs.getString("city"));
                item.setNumber(rs.getInt("number"));
                item.setPrice(rs.getInt("price"));
                item.setPicture(rs.getString("picture"));
                return item;
            } else {
                return null;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        } finally {
            // 释放数据集对象
            if (rs != null) {
                try {
                    rs.close();
                    rs = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            // 释放语句对象
            if (stmt != null) {
                try {
                    stmt.close();
                    stmt = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

        }
    }
    //获取最近浏览的前五条商品信息
    public ArrayList<Items> getViewList(String list)
    {
        //System.out.println("商品ID:list:"+list);
        ArrayList<Items> itemlist = new ArrayList<Items>();
        int iCount=5; //每次返回前五条记录
        if(list!=null&&list.length()>0)
        {
            String[] arr = list.split("#");
          //  System.out.println("arr.length="+arr.length);
            //如果商品记录大于等于5条
            if(arr.length>=5)
            {
               for(int i=arr.length-1;i>=arr.length-iCount;i--)
               {
                  itemlist.add(getItemsById(Integer.parseInt(arr[i])));  
               }
            }
            else
            {
                for(int i=arr.length-1;i>=0;i--)
                {
                    itemlist.add(getItemsById(Integer.parseInt(arr[i])));
                }
            }
            return itemlist;
        }
        else
        {
            return null;
        }

    }

}

index.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%@ page import="entity.Items"%>
<%@ page import="dao.ItemsDAO"%>
<%
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 'index.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">
    -->
    <style type="text/css">
       div{
          float:left;
          margin: 10px;
       }
       div dd{
          margin:0px;
          font-size:10pt;
       }
       div dd.dd_name
       {
          color:blue;
       }
       div dd.dd_city
       {
          color:#000;
       }
    </style>
  </head>

  <body>
    <h1>商品展示</h1>
    <hr>

    <center>
    <table width="750" height="60" cellpadding="0" cellspacing="0" border="0">
      <tr>
        <td>

          <!-- 商品循环开始 -->
           <% 
               Items item = new Items();
               ItemsDAO itemsDao = new ItemsDAO(); 
               ArrayList<Items> list = itemsDao.getAllItems();
               if(list!=null&&list.size()>0)
               {
                   for(int i=0;i<list.size();i++)
                   {
                       item = list.get(i);
           %>   
          <div>
             <dl>
               <dt>
                 <a href="details.jsp?id=<%=item.getId()%>"><img src="images/<%=item.getPicture()%>" width="120" height="90" border="1"/></a>
               </dt>
               <dd class="dd_name"><%=item.getName() %></dd> 
               <dd class="dd_city">产地:<%=item.getCity() %>&nbsp;&nbsp;价格:¥ <%=item.getPrice() %></dd> 
             </dl>
          </div>
          <!-- 商品循环结束 -->

          <%
                   }
              } 
          %>
        </td>
      </tr>
    </table>
    </center>
  </body>
</html>

Items.java

package entity;

//商品类
public class Items {

    private int id; // 商品编号
    private String name; // 商品名称
    private String city; // 产地
    private int price; // 价格
    private int number; // 库存
    private String picture; // 商品图片

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

}

DBHelper.java

package util;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBHelper {

    private static final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //数据库驱动
    //连接数据库的URL地址
    private static final String url="jdbc:sqlserver://localhost:1433;DatabaseName=shopping"; 
    private static final String username="sa";//数据库的用户名
    private static final String password="12345";//数据库的密码

    private static Connection conn=null;

    //静态代码块负责加载驱动
    static 
    {
        try
        {
            Class.forName(driver);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    //单例模式返回数据库连接对象
    public static Connection getConnection() throws Exception
    {
        if(conn==null)
        {
            conn = DriverManager.getConnection(url, username, password);
            return conn;
        }
        return conn;
    }

    public static void main(String[] args) {

        try
        {
           Connection conn = DBHelper.getConnection();
           if(conn!=null)
           {
               System.out.println("数据库连接正常!");
           }
           else
           {
               System.out.println("数据库连接异常!");
           }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

    }
}

sql脚本:

USE [shopping]
GO
/****** Object:  Table [dbo].[items]    Script Date: 03/27/2017 21:23:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[items](
    [id] [int] NOT NULL,
    [name] [char](50) NULL,
    [city] [char](50) NULL,
    [price] [int] NULL,
    [number] [int] NULL,
    [picture] [char](500) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[items] ([id], [name], [city], [price], [number], [picture]) VALUES (1, N'沃特篮球鞋                                        ', N'佛山                                              ', 180, 500, N'001.jpg                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ')
INSERT [dbo].[items] ([id], [name], [city], [price], [number], [picture]) VALUES (2, N'安踏运动鞋                                        ', N'福州                                              ', 120, 800, N'002.jpg                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ')
INSERT [dbo].[items] ([id], [name], [city], [price], [number], [picture]) VALUES (3, N'耐克运动鞋                                        ', N'广州                                              ', 500, 1000, N'003.jpg                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ')
INSERT [dbo].[items] ([id], [name], [city], [price], [number], [picture]) VALUES (4, N'阿迪达斯T血衫                                     ', N'广州                                              ', 180, 900, N'005.jpg                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ')
INSERT [dbo].[items] ([id], [name], [city], [price], [number], [picture]) VALUES (5, N'李宁文化衫                                        ', N'佛山                                              ', 180, 500, N'001.jpg                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ')
INSERT [dbo].[items] ([id], [name], [city], [price], [number], [picture]) VALUES (6, N'小米3                                             ', N'北京                                              ', 1999, 3000, N'006.jpg                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ')
INSERT [dbo].[items] ([id], [name], [city], [price], [number], [picture]) VALUES (7, N'小米2S                                            ', N'北京                                              ', 1299, 1000, N'007.jpg                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ')
INSERT [dbo].[items] ([id], [name], [city], [price], [number], [picture]) VALUES (8, N'thinkpad笔记本                                    ', N'北京                                              ', 6999, 500, N'008.jpg                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ')
INSERT [dbo].[items] ([id], [name], [city], [price], [number], [picture]) VALUES (9, N'dell笔记本                                        ', N'北京                                              ', 3999, 500, N'009.jpg                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ')
INSERT [dbo].[items] ([id], [name], [city], [price], [number], [picture]) VALUES (10, N'ipad5                                             ', N'北京                                              ', 5999, 500, N'010.jpg                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ')

该项目使用了:

**1. 将URL传递的id保存起来就可以了。因为id是唯一的,通过id就能找回一件商品的所有信息。
2. 利用String类型的分隔符方式存储id,再以分隔符的方式得到字符串数组。(String的指定分隔符返回字符串数组)**

posted @ 2017-03-28 13:19  练涛  阅读(378)  评论(0编辑  收藏  举报