Servlet 3.1 所有表单参数

Java Servlet 中的 HttpServletRequest 实例的 getParameterNames() 方法可以读取所有可用的表单参数

getParameterNames() 该方法返回一个枚举,其中包含未指定顺序的参数名

然后我们就可以以标准方式循环枚举,使用 hasMoreElements() 方法来确定何时停止,使用 nextElement() 方法来获取每个参数的名称

范例

// author: 简单教程(www.twle.cn)
// Copyright © 2015-2065 www.twle.cn. All rights reserved.

package cn.twle.demo;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*; 
import javax.servlet.annotation.WebServlet;
import java.util.Enumeration;

@WebServlet(name = "ReadAllParamsServlet", urlPatterns = {"read_all_params"}, loadOnStartup = 1) 
public class ReadAllParamsServlet extends HttpServlet
{

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String title = "读取所有的表单数据 | 简单教程(www.twle.cn)";
        String docType =
            "<!doctype html>";
            out.println(docType +
            "<meta charset=\"utf-8\"><title>" + title + "</title>" +
            "<body bgcolor=\"#f0f0f0\">\n" +
            "<p>" + title + "</p>\n" +
            "<table width=\"100%\" border=\"1\">\n" +
            "<tr bgcolor=\"#949494\">\n" +
            "<th>参数名称</th><th>参数值</th>\n"+
            "</tr>\n");

        Enumeration paramNames = request.getParameterNames();

        while(paramNames.hasMoreElements()) {
            String paramName = (String)paramNames.nextElement();
            out.print("<tr><td>" + paramName + "</td>\n");
            String[] paramValues =
            request.getParameterValues(paramName);
            // 读取单个值的数据
            if (paramValues.length == 1) {
                String paramValue = paramValues[0];
                if (paramValue.length() == 0)
                    out.println("<td><i>没有值</i></td>");
                else
                    out.println("<td>" + paramValue + "</td>");
            } else {
                // 读取多个值的数据
                out.println("<td><ul>");
                for(int i=0; i < paramValues.length; i++) {
                out.println("<li>" + paramValues[i]);
            }
                out.println("</ul></td>");
            }
            out.print("</tr>");
        }
        out.println("\n</table>\n</body>");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

get_all_params.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" %>  
<!DOCTYPE html>
<meta charset="UTF8">
<meta http-equiv=Content-Type content="text/html;charset=UTF8">
<title>获取所有表单参数 | 简单教程(www.twle.cn)</title>
<p>获取所有表单参数 | 简单教程(www.twle.cn)</p>
<form action="read_all_params" method="POST">
<input type="checkbox" name="maths" checked="checked" /> 数学
<input type="checkbox" name="physics"  /> 物理
<input type="checkbox" name="chemistry" checked="checked" /> 化学
<input type="submit" value="选择学科" />
</form>

在浏览器上输入 http://localhost:8080/servlet/get_all_params.jsp 显示结果如下

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.