这是我的Servlet上的一个用于测试各种功能的函数(尽管我对逻辑不了解,但我是servlet的新手)
public void testParameters(HttpServletRequest request, HttpServletResponse response) throws IOException{
PrintWriter out = response.getWriter();
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
out.println("\n>>>" + paramName);
String[] paramValues = request.getParameterValues(paramName);
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() == 0){
out.print("No Value");
}else{
out.print(paramValue);
}
} else {
System.out.println("Number of parameters "+paramValues.length);
for(int i=0; i<paramValues.length; i++) {
out.print("" + paramValues[i]);
}
}
}
}
((此代码是我从教程中提取的,所以tweeked,所以可能只是愚蠢的东西)
我一切正常,但是我在什么情况下徘徊参数有多个值?
示例:http://myhost/path?a=b&a=c&a=d
参数a具有值b,c和d。
示例:
<form name="checkform" method="post" action="xxxxx">
Which langauge do you want to learn:<br>
<input type="checkbox" name="langtype" value="JSP">JSP
<input type="checkbox" name="langtype" value="PHP">PHP
<input type="checkbox" name="langtype" value="PERL">PERL
<input type="submit" name="b1" value="submit">
</form>
该表格可以让您选择多个值。如果您勾选所有复选框,则参数langtype将具有值JSP,PHP和PERL