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 越界异常是怎么回事,完全看不出问题神,求指教