遍历请求头
Enumeration<String> headerNames = request.getHeaderNames();
if (null != headerNames) {
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
String headerValue = request.getHeader(headerName);
headerMap.put(headerName, headerValue);
}
}
遍历cookie
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
CookieVo cookieVo = new CookieVo();
cookieVo.setComment(cookie.getComment());
cookieVo.setDomain(cookie.getDomain());
cookieVo.setHttpOnly(true);
cookieVo.setMaxAge(cookie.getMaxAge());
cookieVo.setName(cookie.getName());
cookieVo.setPath(cookie.getPath());
cookieVo.setSecure(cookie.getSecure());
cookieVo.setValue(cookie.getValue());
cookieVo.setVersion(cookie.getVersion());
cookiesLists.add(cookieVo);
}
}
遍历param
Enumeration<String> parameterNames = request.getParameterNames();
if (null != parameterNames) {
while (parameterNames.hasMoreElements()) {
String parameterName = parameterNames.nextElement();
String parameterValue = request.getParameter(parameterName);
requestMap.put(parameterName, parameterValue);
}
}
遍历attribute
Enumeration<String> attributeNames = request.getAttributeNames();
if (null != attributeNames) {
while (attributeNames.hasMoreElements()) {
String attributeName = attributeNames.nextElement();
String attributeValue = request.getAttribute(attributeName).toString();
attributeMap.put(attributeName, attributeValue);
}
}
获取请求的全路径,解码,获取其中的参数
StringBuffer requestURL = request.getRequestURL();
String queryString = request.getQueryString();
String url = requestURL.toString() + "?" + queryString;
@SuppressWarnings("deprecation")
String decodeUrl = URLDecoder.decode(url);
urlParam = this.queryString(decodeUrl);
private Map<String, Object> queryString(String urlStr) {
Map<String, Object> queryMap = new HashMap<>();
URL url;
try {
url = new URL(urlStr);
String queryStr = url.getQuery();
String[] queryArr = queryStr.split("[&]");
for (String queryItem : queryArr) {
String[] arrSplitEqual = null;
arrSplitEqual = queryItem.split("[=]");
if (arrSplitEqual.length > 1) {
queryMap.put(arrSplitEqual[0], queryItem.substring(arrSplitEqual[0].length() + 1));
} else {
if (arrSplitEqual[0] != "") {
queryMap.put(arrSplitEqual[0], "");
}
}
}
return queryMap;
} catch (MalformedURLException e) {
e.printStackTrace();
}
return queryMap;
}