login.jsp<form method="post" action="login">
<input type="submit" value="提交" />
=============================================
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>
=============================================
BookDao.javapublic List<TBook> selevtAllBook(){
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<TBook> bookList = new ArrayList<TBook>(); conn = this.getConn();
pstmt = conn.prepareStatement("select * from book");
rs = pstmt.executeQuery();
while(rs.next()){
TBook book = new TBook();
book.setBookName(rs.getString("bookName"));
book.setIsbn(rs.getString("isbn"));
book.setAuthor(rs.getString("author"));
book.setPublisher(rs.getString("publisher"));
book.setBookPrice(rs.getFloat("bookPrice"));
book.setDiscount(rs.getDouble("discount"));
book.setBookID(rs.getInt("bookID"));
bookList.add(book);
}
return bookList;
}
=============================================
loginservlet.java
BookDao bookDao = new BookDao();
List<TBook> bookList = bookDao.selevtAllBook();request.setAttribute("id", bookList.get(6));
request.setAttribute("bookList", bookList);
request.getRequestDispatcher("bookList.jsp").forward(request, response);我该如何在bookList.jsp这个页面,或者是在booklistservlet中得到bookList中的那个bookID我在bookListservlet中写request.getParameter("id")怎么不行啊,得到的是null;

解决方案 »

  1.   

    bookListservlet中写request.getParameter("id")怎么不行啊,得到的是null;那你需要 在 url后增加 &id=111
    或者你页面中增加一个 hidden的元素赋值给它
      

  2.   

    request.getRequestDispatcher("bookList.jsp?id="+bookList.get(6)).forward(request, response);这样可以吗?
      

  3.   

    我在loginservlet中写了那个request.setAttribute("id", bookList.get(6));不行么
      

  4.   

    在你的form中添加隐藏<input type="hidden" name="id" value="" />
    你都没有把id放在你的提交参数中怎么能接收到呢
      

  5.   

    那么value中怎么得到booklist中的bookList.get(6)那个值呢
      

  6.   

    你应该用request.getAttribute("id"),而不是request.getParameter("id"),request.getParameter是获得get方式的传值的,比如url?id=123&name=cc
      

  7.   

    bookList.get(6) ?? 
    你能确定 bookList 里有7条数据么?
      

  8.   

    你把list遍历下看看有几条数据
      

  9.   

    bookListservlet中要通过request.getParameter("id")获取参数值,则该参数必须是通过表单传递的。所以,请确认,你的id是放在表单中。
      

  10.   

    你应该用request.getAttribute("id"),而不是request.getParameter("id"),request.getParameter是获得get方式的传值的,比如url?id=123&name=cc
    引用楼主 ericfung 的回复:
    login.jsp<form method="post" action="login">
    <input type="submit" value="提交" />
    =============================================
    <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    ……
     
     
     
     
    +
      

  11.   

    1.bookList.get(6)是获取你bookList中的第六条Tbook对象不是BookID,所以你的首先看你bookList中是否大于等于六条数据否则就是null,通过bookList.get(6).getBookID()才能得到BookID
    2.request.setAttribute()用request.getParameter()取不出来如果在页面取可以使用el表达式
    ${id}取值,在servlet中用request.getAttribute(id)取值;
      

  12.   

    request.setAttribute("id", bookList.get(6));
    只是把bookList集合中的第7个对象放到request中
    request.setAttribute("id", bookList.get(6).getBookID());
    才是把bookList集合中的第7个对象的BookId放到request中至于在servlet中接受页面中传递来的id用request.getParameter("id");
      

  13.   

    楼上说的对, 你放进去的是TBook对象,怎么可以当成id取呢
     另外就是这个list的size 事多少,你打印一下看看
      

  14.   

    你应该用request.getAttribute("id"),而不是request.getParameter("id"),request.getParameter是获得get方式的传值的,比如url?id=123&name=cc
    引用楼主 ericfung 的回复:
    login.jsp<form method="post" action="login">
    <input type="submit" value="提交" />
    =============================================
    <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
      

  15.   


    request.getParameter不管get还是post都能获取到好吧,request.getAttribute获取的是对象。
    lz用form表单post方式提交可以用request.getParameter("id")得到!!