本系列博客汇总在这里:请求对象 request 和响应对象 response 汇总


目录


request 的 getParameterMap 方法详解

  1. 获取所有参数对应的 Map,其中 key 为参数名,value 为参数值。

  2. 示例
    HelloServlet
    请求对象 request 和响应对象 response(5)_request 的 getParameterMap 方法详解_java
    web.xml
    请求对象 request 和响应对象 response(5)_request 的 getParameterMap 方法详解_xml_02
    1.html
    请求对象 request 和响应对象 response(5)_request 的 getParameterMap 方法详解_html_03
    结果
    请求对象 request 和响应对象 response(5)_request 的 getParameterMap 方法详解_java_04

  3. 以上操作完整源码
    HelloServlet

    package com.wyx.servlet;
    import java.io.IOException;
    import java.util.Map;
    import java.util.Set;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public class HelloServlet extends HttpServlet
    {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
    // 获得表单中所有文本域的name和值以map方式来存储
    Map map = request.getParameterMap();
    // 获得map的key(就是表单中文本域的name集合)
    Set<String> set = map.keySet();
    for (String name : set)
    {
    // 获得每个文本域所对应的值
    String[] vals = (String[]) map.get(name);
    System.out.print(name + ":");
    for (String val : vals)
    {
    System.out.print(val + " ");
    }
    System.out.println();
    }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
    {
    doGet(req, resp);
    }
    }

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>com.wyx.servlet.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/hello</url-pattern>
    </servlet-mapping>

    </web-app>

    1.html

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>

    <h1>表单方式来提交数据</h1>
    <form action="/reqresp_demo2/hello" method="post">
    姓名:<input name="name" type="text"><br>
    年龄:<input name="age" type="text"><br>
    性别:<input name="gender" type="radio" value="1">&nbsp;
    <input name="gender" type="radio" value="0">
    <br>
    职位:<select name="job">
    <option value="1">教授</option>
    <option value="2">副教授</option>
    </select>
    <br>
    简介:<textarea rows="10" cols="20" name="introduce"></textarea>
    <br>
    爱好:<input name="favor" type="checkbox" value="1">篮球&nbsp;
    <input name="favor" type="checkbox" value="2">足球&nbsp;
    <input name="favor" type="checkbox" value="3">羽毛球&nbsp;
    <br>
    <input type="submit" value="提交">
    </form>
    </body>
    </html>

    工程文件下载

如有错误,欢迎指正!