Servlet 读取 HTML 中同一 NAME 属性值的多个参数

来源:互联网 发布:匡恩网络内部融资 编辑:程序博客网 时间:2024/06/16 11:38

我们通常读取的都是根据 HTML 中的标签的不同的 NAME 值来获取 Parameter 的,但是,如果有多个标签是相同的 NAME  值呢?比方说 HTML 如下的网页:

 
   1:  <form method="post" action="login">
   2:      <div>Username:<input class="input" type="text" name="name"/><br/>div>
   3:      <div>Password:<input class="input" type="text" name="name"/><br/>div>
   4:      <input type="submit" value="Submit"/>
   5:  form>    
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

此时我们可以参照如下代码解决问题:

   1:  String[] values = req.getParameterValues("name");
   2:  if (values.length == 1) {
   3:      if (values[0].length() != 0) {
   4:          resp.getWriter().println("" + values[0] + "");
   5:      } else {
   6:          resp.getWriter().println("空值");
   7:      }
   8:  } else {
   9:      for (String s : values) {
  10:          resp.getWriter().println("" + s + "");
  11:      }
  12:  }
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

也就是调用 getParameterValues("name") 来读取多个值。

此外,我们还可以通过 getParameterMap() 来实现:

   1:  Map parasMap = req.getParameterMap();
   2:  Set> parasSet = parasMap.entrySet();
   3:  for (Map.Entry s : parasSet) {
   4:      String[] values = s.getValue();
   5:      for (String val : values) {
   6:          if (val.length() != 0) {
   7:              resp.getWriter().println("

" + val + "

"
);
   8:          } else {
   9:              resp.getWriter().println("

空值

"
);
  10:          }
  11:      }
  12:  }
 

getParameterMap

 java.util.Map getParameterMap()
Returns a java.util.Map of the parameters of this request. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.
Returns:
an immutable java.util.Map containing parameter names as keys and parameter values as map values. The keys in the parameter map are of type String. The values in the parameter map are of type String array.

原创粉丝点击