读书人

怎么向一个jsp传递两个以上的参数如

发布时间: 2012-02-13 17:20:26 作者: rapoo

如何向一个jsp传递两个以上的参数,如商品的名称和数量?(商品的名称和数量取自数据库 是动态的)
如何向一个jsp(orderform.jsp)传递两个以上的参数,如商品的名称和数量?(商品的名称和数量取自数据库 是动态的)
用checkbox只能取得商品名称,如何把本行的商品数量也传递过去呢?忘各位大虾帮忙,小弟不胜感激;)

////////////////////////product.jsp
<%@ page language= "java " import= "java.util.*,java.sql.* " pageEncoding= "ISO-8859-1 "%>

<jsp:useBean id= "con " scope= "session " class= "test.DBCon "/>
<html>
<head>
<title> Welcome to products page </title>
</head>
<%@include file= "Title.html " %>
<body>
<%
con.getCon();
String action=request.getParameter( "action ");
String pname=request.getParameter( "pname ");
String pnum=request.getParameter( "num ");
String price=request.getParameter( "price ");
try{
String username=session.getValue( "name ").toString();
if(username==null){%>
you haven 't login <%}
else{
%> Welcome <%=username%> ,the time now is <%=new java.util.Date().toString()%>
<a href= "AlterUserInformation.jsp "> Alter my information </a> <%
}
}catch(Exception ex){
out.println( "you haven 't login,please back to Login Page ");
}%>


<form action= "orderform.jsp " name= "form2 ">
<table bgcolor= "#708491 ">
<tr>
<td width= "25% "> Product Name </td> <td width= "25% "> Price </td>
</tr>
<%
Statement stmt=con.getStmtread();
ResultSet rs=stmt.executeQuery( "select * from product ");
while(rs.next()){%>
<tr>
<td width= "20% "> <%=rs.getString( "ProdName ")%> </td>
<td width= "20% "> <%=rs.getFloat( "Price ")%> </td>
<td width= "15% "> Number: </td>
<td width= "10% "> <input size=2 type= "text " id= "num " name= "num " value= "1 "> </td>
<td width= "10% "> <input size=2 type= "checkbox " id= "want " name= "want " value= <%=rs.getString( "ProdName ")%> > </td>
</tr>


<%}
rs.close();
stmt.close();
%>
<tr>
<input type= "submit " value= "confirm "> </input>
</tr>
</table>
</form>
<%
if(pname!=null && action.equals( "addtocart ")){
out.println(pname+ " X "+pnum+ " have been put into your shopping cart. ");

}
%>


</body>
</html>
//////////////////////////////////orderform.jsp///////////////////
<%@ page language= "java " import= "java.util.* " pageEncoding= "ISO-8859-1 "%>

<html>
<head>
<title> this is your orderform </title>
</head>
<body>
<%String[] pname=request.getParameterValues( "want ");
String[] num=request.getParameterValues( "num ");

if(pname!=null&&pname.length!=0){
out.println( "you have choose "+ " <br> ");
for(int i=0;i <pname.length;i++){
out.println(pname[i]+ "* "+num[i]+ " <br> ");
}
}

%>
</body>
</html>



[解决办法]
url= "../orderform.jsp?shuliang=xx "
其实传2个参数,你只要放在from里在下一个页面获值就完全可以
[解决办法]
url= "../orderform.jsp?shuliang=xx&xxx=xxx "

用&号连接
[解决办法]
?号第一个参数,&连接以后的参数
[解决办法]
有两种传参方式
一、get方式:
这种方式将参数写在url中,举例来说,如果目标传参页面是target.jsp,那么在url里可以这么写:
http://..../target.jsp? <Param1> = <value1> & <Param2> = <value2> &...
可以传递多个参数。
在target.jsp中,用request.getParameter( " <ParamName> ");获取;
二、post方式
这种方式是用 <form> 框体传递参数。举例来说,如需要将参数由source.jsp传递到target.jsp。那么在source.jsp中需要这么写:
.......
<form name= "form1 " action= "target.jsp " method= "post ">
<input name= " <Param1> " value= " <value1> " type= "text ">
<input name= " <Param2> " value= " <value2> " type= "hidden ">
<!--用input标签的hidden类型,那么这个input标签在页面显示时不会被显示,但该参数回传递到form的action目标页面去,这个方法可能对你传参有用-->
.....
</form>
在target.jsp端同样用request.getParameter( " <ParamName> ");获取;
[解决办法]
值放在FORM中多少值都能拿到
------解决方案--------------------


hansheng9822(鼾声)

补充 post方法为了页面好看用hidden域
[解决办法]
while(rs.next())
{%>
<tr>
<td width= "20% "> <%=rs.getString( "ProdName ")%> </td>
<td width= "20% "> <%=rs.getFloat( "Price ")%> </td>
<td width= "15% "> Number: </td>
<td width= "10% "> <input size=2 type= "text " id= "num " name= "num " value= "1 "> </td>
<td width= "10% "> <input size=2 type= "checkbox " id= "want " name= "want " value= <%=rs.getString( "ProdName ")%> > </td>
</tr>
<%}
------------------------
上面代码循环出来的want和num标签个数不是相同的吗?
lz说的:“如何匹配这些数量跟商品,商品只取过来3个,但是商品的数量有5个”是什么意思?
[解决办法]
<td width= "10% "> <input size=2 type= "text " id= <%rs.getString( "ProdName ")%> name= "num " value= "1 "> </td>

String[] pname=request.getParameterValues( "want ");
for(int i=0;u <pname[].length;i++)
String[] num=request.getParameterValues(pname[i]);

[解决办法]
你为什么不把名称和数量绑定呢
在一个CHECKBOX 下把他的VALUE值用逗号分开,然后在下一个页面接收,然后解析出来,这样的话就不会有对号出错的问题

读书人网 >Java Web开发

热点推荐