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>");
 }
}

 

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

 

施杨出品!!!

posted on 2008-07-25 21:16  俱正Steven  阅读(1824)  评论(1编辑  收藏  举报

导航