HttpServletRequest类常见API
- javax.servlet.http.HttpServletRequest与HttpServletResponse类解析
/*** Copyright (c) bigbeardhk@163.com Corporation 2022. All Rights Reserved.*/package com.hk.study.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.util.Enumeration;import java.util.Map;/*** @Author : bigbeardhk* @Date : 2022/06/04 21:12**/@RestController@RequestMapping("/httpServlet")public class HttpServletController {@GetMapping("/testRequest/{id}")public String testRequest(HttpServletRequest request, HttpServletResponse response) {StringBuffer requestURL = request.getRequestURL(); // http://localhost:8081/httpServlet/testRequest/654String queryString = request.getQueryString(); // ?后面的地址参数 test=test-51gjie&u2=u2-51gjietMap<String, String[]> parameterMap = request.getParameterMap(); // ?后面的参数和请求体里面的参数Cookie[] cookies = request.getCookies();HttpSession session = request.getSession();session.setAttribute("sessionname","sessionname");String jsessionid = (String) session.getAttribute("JSESSIONIDTest");// ====================response=============================response.addHeader("token", "dsfsdfsdhthytju"); // 响应头response.addCookie(new Cookie("myname", session.getId())); // set-cookiesreturn "helloword";}}
/**
* Copyright (c) bigbeardhk@163.com Corporation 2022. All Rights Reserved.
*/
package com.hk.study.util;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
/**
* @Author : bigbeardhk
* @Date : 2022/05/03 18:41
**/
public class AddressUtil {
/**
* 获取请求的ip地址
*
* @param request
* @return
*/
public static String getIpAddress(HttpServletRequest request) {
String ipAddress = null;
try {
ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if (ipAddress.equals("127.0.0.1")) {
// 根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
}
ipAddress = inet.getHostAddress();
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
// = 15
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
} catch (Exception e) {
ipAddress = "";
}
// ipAddress = this.getRequest().getRemoteAddr();
return ipAddress;
}
/**
* 获取完整的请求地址
* @param request
* @return
*/
public static String getRequestAddress(HttpServletRequest request) {
StringBuffer requestURL = request.getRequestURL();
String completeURL = requestURL.toString();
String queryString = request.getQueryString(); // ?后面的地址参数
if (StringUtil.isNotEmpty(queryString)) {
completeURL = completeURL + "?" + queryString;
}
return completeURL;
}
}
常见问题
:::info
Q :如何生成cookies?
A : 可以在响应时,set-cookies,并为cookies设置过期时间,如果Expires为会话时,说明浏览器一关闭,该cookies就会失效
Q :cookies的跨域区分标准为?
A:协议 + ip + 端口号 ,如https://localhost:8080/
Q :cookies的不可跨域性?
A:可以给cookies设置domain与path参数,记住domain参数必须以点(“.”)开始;path属性需要使用符号“/”结尾
K : JSESSIONID是tomcat容器默认给sessionID,set-cookies的key :::
资料学习与引用

