zoukankan      html  css  js  c++  java
  • requset获取post提交的请求参数

    1.请求体的内容通常是通过post来提交的,格式是

    username=zhansan&password=123&hobby=football||&hobby=basketball

       key--------------------------------value

      username                             [zhangsan]

      password                              [123]

      hobby                                    [football,basketball]

    2.定义和用法

    // 1.获取单个表单中的value值
            String usernam = request.getParameter("usernam");
            String password = request.getParameter("password");
    // 2.获取多个表单的value值
            String[] parameterValues = request.getParameterValues("hobby");
            System.out.print("hobby.values:");
    // 3.获得所有参数的name
                Enumeration<String> parameterNames = request.getParameterNames();
                for (String string2 : parameterValues) {
                    System.out.println(parameterNames.nextElement());
    
                }

    // 4.将获得的所有参数封装到一个Map<String,String[]>
    Map<String, String[]> parameterMap = request.getParameterMap();
            for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
                System.out.println(entry.getKey());
                for (String string : entry.getValue()) {
                    System.out.println(string);
                }
                System.out.println("----------------");
            }

    3.实例结果

    4.完整代码

    package com.hdh.requesd;
    
    import java.io.IOException;
    import java.util.Enumeration;
    import java.util.Map;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class ContentServlet extends HttpServlet {
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            // 1.获取单个表单中的value值
            String usernam = request.getParameter("usernam");
            String password = request.getParameter("password");
            System.out.println("usernam.value:" + usernam);
            System.out.println("password.value:" + password);
            // 2.获取多个表单的value值
            String[] parameterValues = request.getParameterValues("hobby");
            System.out.print("hobby.values:");
            for (String string : parameterValues) {
                System.err.print(string + "  ");
            }
            System.out.println("---------------------");
            // 3.获得所有参数的name
            Enumeration<String> parameterNames = request.getParameterNames();
            for (String string2 : parameterValues) {
                System.out.println(parameterNames.nextElement());
    
            }
            System.out.println("---------------------");
            // 4.将获得的所有参数封装到一个Map<String,String[]>中
            Map<String, String[]> parameterMap = request.getParameterMap();
            for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
                System.out.println(entry.getKey());
                for (String string : entry.getValue()) {
                    System.out.println(string);
                }
                System.out.println("----------------");
            }
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doGet(request, response);
        }
    
    }

    form.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>   
        <hr>
        <form action="/WEB15/content" method="post">
        姓名:<input type="text" name="username"><br>
        密码:<input type="password" name="password"><br>
        <input type="checkbox" name="hobby" value="zq">足球
        <input type="checkbox" name="hobby" value="lq">篮球
        <input type="checkbox" name="hobby" value="ppq">乒乓球
        <input type="submit" value="提交">
        </form>
    </body>
    </html>

     

  • 相关阅读:
    node 命令
    nodejs项目搭建
    linux 安装与配置
    GestureDetector
    activity切换效果
    hadoop
    phonegap 自定义插件
    自定义控件-属性自定义
    zxing demo
    select 语句的执行顺序
  • 原文地址:https://www.cnblogs.com/asndxj/p/9828894.html
Copyright © 2011-2022 走看看