Java 怎么从httpservletrequest获取请求映射(Map)

2019年8月17日22:03:31 评论 30

1.简介

Java 怎么从httpservletrequest获取请求映射(Map)?——请看下文。

2.代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Main{
    
    public static Map<String, String> getRequestMap(
            HttpServletRequest request) {
        Map<String, String> params = new HashMap<String, String>();
        Enumeration<String> parameters = request.getParameterNames();
        while (parameters.hasMoreElements()) {
            String key = parameters.nextElement();
            String value = request.getParameter(key);
            value = value == null ? "" : value;
            params.put(key, value);
        }
        return params;
    }
}

 

3.总结

示例代码演示了 Java 怎么从httpservletrequest获取请求映射(Map),具体步骤参见示例代码。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: