jsp如何向servlet发送请求参数
我是jsp新手,刚刚构建了我的第一个应用程序。我在看一本书。这本书使用了密码jsp如何向servlet发送请求参数,jsp,Jsp,我是jsp新手,刚刚构建了我的第一个应用程序。我在看一本书。这本书使用了密码 <form name="addForm" action="ShoppingServlet" method="post"> <input type="hidden" name="do_this" value="add"> Book: <select name="book"> <% //S
<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()
else if(做这个等于(“添加”)){
布尔值=false;
Book aBook=getBook(请求);
如果(shopList==null){//,则购物车为空
shopList=newarraylist();
购物清单。添加(aBook);
}否则{…}//如果书已经存在,请更新#份数
私有图书getBook(HttpServletRequest){
字符串myBook=request.getParameter(“book”);//混乱
int n=myBook.indexOf(“$”);
字符串title=myBook.substring(0,n);
字符串价格=myBook.substring(n+1);
字符串数量=request.getParameter(“数量”);//混乱
返回新书(标题,Float.parseFloat(价格),Integer.parseInt(数量));
}//getBook()的结尾
我的问题是,当我单击AddAddtoCart按钮,然后在servelt的第行字符串myBook=request.getParameter(“book”);
中,我将book作为一个参数获取,但在我的jsp中,我没有说request.setAttribute(“book”,“book”)
,与request.getParameter(“qty”)相同;
。我的servlet如何在不在jsp代码中设置的情况下接收这些请求参数?
谢谢您获得了该参数,因为在您的表单中有以下内容:
<select name="book">
上面将创建两个名为
abc
和age
的请求参数,您可以使用request来访问它们。getParameter
hhmm您的意思是,如果在我的表单中我使用带有name属性(name=“xxx”)的html元素然后所有名称属性值都自动添加到httpServletRequest中。是吗?但您必须了解请求参数
和请求属性
之间的区别。请求参数
和请求属性
之间的区别是什么?据我所知,您是否要设置属性您可以使用request.setAttribute(name,value)
和获取request.getParameter(name)
…我说得对吗?我还应该知道什么?ThanksA请求参数是浏览器发送到服务器的内容。请求属性由服务器在服务器上设置。有关详细信息,我建议您进行搜索。
http://localhost:8080/ShoppingServlet?name=abcd&age=20