Servlet的Web应用---三种方法获取表单的各种属性(源码)

一、最通俗的方法:request.getParameter("name属性")

 

源码:

 

public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("text/html;charset=utf-8");
  PrintWriter out = response.getWriter();
  out
    .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
  out.println("<HTML>");
  out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
  out.println("  <BODY>");
  request.setCharacterEncoding("utf-8");
  out.println("姓名:"+filterHtml(request.getParameter("name"))+"<br>");
  out.println("Email:"+filterHtml(request.getParameter("email"))+"<br>");
  out.println("年龄:"+request.getParameter("age")+"<br>");
  out.println("编程时间:"+request.getParameter("codetime")+"<br>");
  out.println("操作系统:");
  String  os[]=request.getParameterValues("os");
  out.println("<ul>");
  for(int i=0;i<os.length;i++){
   out.println("<li>"+os[i]+"</li>");
  }
  out.println("</ul><br>");
  out.println("编程语言:");
  String  language[]=request.getParameterValues("language");
  out.println("<ul>");
  for(int i=0;i<language.length;i++){
   out.println("<li>"+language[i]+"</li>");
  }
  out.println("</ul><br>");
  out.println("建议:"+filterHtml(request.getParameter("comment"))+"<br>");
  out.println("</BODY>");
  out.println("</HTML>");
  out.flush();
  out.close();
 }

 

此法缺点:需要知道表单的name属性。以下两种不需要知道,就可以。

 

二、Enumeration e = request.getParameterNames()方法:

 

public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setContentType("text/html;charset=utf-8");
  PrintWriter out = response.getWriter();
  request.setCharacterEncoding("utf-8");

  out.println("<html>");
  out.println("<head><title>display survey infomation</title></head>");
  out.println("<body>");
  out.println("<h2>用户输入信息:</h2>");

  String parameterName = null;
  Enumeration e = request.getParameterNames();
  while (e.hasMoreElements()) {
   parameterName = (String) e.nextElement();
   out.println("参数名称:" + parameterName + "<BR>");
   printValues(out, request.getParameterValues(parameterName));
  }
  out.flush();
  out.close();
 }

 public void printValues(PrintWriter out, String[] values) {
  out.println("<ul>");
  for (int i = 0; i < values.length; i++) {
   out.println("<li>" + values[i] + "</li>");
  }
  out.println("</ul>");
 }
}

 

三、数组方法:

 

public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setContentType("text/html;charset=utf-8");
  PrintWriter out = response.getWriter();
  request.setCharacterEncoding("utf-8");

  out.println("<html>");
  out.println("<head><title>display survey infomation</title></head>");
  out.println("<body>");
  out.println("<h2>用户输入信息:</h2>");

  String parameterName = null;
  Map map = request.getParameterMap();
  Set set=map.keySet();
  Iterator it=set.iterator();
  while (it.hasNext()) {
   parameterName = (String) it.next(); 
   out.println("参数名称:" + parameterName + "<BR>");
   printValues(out, request.getParameterValues(parameterName));
  }
  out.flush();
  out.close();
 }

 public void printValues(PrintWriter out, String[] values) {
  out.println("<ul>");
  for (int i = 0; i < values.length; i++) {
   out.println("<li>" + values[i] + "</li>");
  }
  out.println("</ul>");
 }
}

 

源码测试通过,准确!!!


本文转自施杨博客园博客,原文链接:http://www.cnblogs.com/shiyangxt/archive/2008/07/25/1251706.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值