我是 jsp 新手,刚刚构建了我的第一个应用程序。我正在关注一本书。这本书使用了一个代码
<form name="addForm" action="ShoppingServlet" method="post">
<input type="hidden" name="do_this" value="add">
Book:
<select name="book">
<%
//Scriplet2: copy the booklist to the selection control
for (int i=0; i<bookList.size(); i++) {
out.println("<option>" + bookList.get(i) + "</option>");
} //end of for
%>
</select>
Quantity:<input type="text" name="qty" size="3" value="1">
<input type="submit" value="Add to Cart">
</form>
在servlet中,代码是
else if(do_This.equals("add")) {
boolean found = false;
Book aBook = getBook(request);
if (shopList == null) { // the shopping cart is empty
shopList = new ArrayList<Book>();
shopList.add(aBook);
} else {... }// update the #copies if the book is already there
private Book getBook(HttpServletRequest request) {
String myBook = request.getParameter("book"); //confusion
int n = myBook.indexOf('$');
String title = myBook.substring(0, n);
String price = myBook.substring(n + 1);
String quantity = request.getParameter("qty"); //confusion
return new Book(title, Float.parseFloat(price), Integer.parseInt(quantity));
} //end of getBook()
我的问题是,当我单击添加到购物车按钮时,然后在 serverlt 行中String myBook = request.getParameter("book");
我将 book 作为参数,但在我的 jsp 中我没有这么说request.setAttribute("book", "book")
,对于request.getParameter("qty");
. 我的 servlet 如何在没有在 jsp 代码中设置的情况下接收这些请求参数?谢谢