package cn.itcast.cookie;import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class CookieDemo3 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();

//1.显示网站所有商品
out.write("本网站有如下书籍:<br><br>");
Set<Map.Entry<String,Book>> set=DB.getAll().entrySet();
for(Map.Entry<String,Book> me :set){
Book book =me.getValue();
out.write("<a href='/myday07/servlet/CookieDemo4?id="+book.getId()+"'>"+book.getName()+"</a>");
out.write("<br/>");
} } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doGet(request, response);
}}
//模拟存放书的数据库
class DB{
private static Map<String,Book> map=new HashMap();
static{
map.put("1",new Book("1","javaweb开发","老张","一本好书"));
map.put("1",new Book("2","spring开发","老黎","一本好书"));
map.put("1",new Book("3","hibernate开发","老李","一本好书"));
map.put("1",new Book("4","struts开发","老毕","一本好书"));
map.put("1",new Book("5","ajax开发","老王","一本好书"));
}
public static Map getAll(){
return map;
}
}
//创建书对象封装数据
class Book{
private String id;
private String name;
private String author;
private String discription;

public Book() {
super();
}

public Book(String id, String name, String author, String discription) {
super();
this.id = id;
this.name = name;
this.author = author;
this.discription = discription;
}
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 getDiscription() {
return discription;
}
public void setDiscription(String discription) {
this.discription = discription;
}

}这是一个servlet程序,为什么我写的就显示一本书呢???看了几遍实在没发现问题servlet

解决方案 »

  1.   

    这么明显的错误你竟然没看出来?!!
    map.put("1",new Book("1","javaweb开发","老张","一本好书"));
    map.put("1",new Book("2","spring开发","老黎","一本好书"));
    map.put("1",new Book("3","hibernate开发","老李","一本好书"));
    map.put("1",new Book("4","struts开发","老毕","一本好书"));
    map.put("1",new Book("5","ajax开发","老王","一本好书"));key值就一个。。你说呢
      

  2.   

    这个也太明显了啊,你第一次是添加,后面都在修改这个key对应的值,最后当然只有一个啊、、、、