在Java Web开发中,request对象是一个表示客户端请求的对象,它封装了客户端发送给服务器的HTTP请求信息,通过request对象,我们可以获取客户端的请求头、请求参数、请求方法等信息,在Servlet和JSP中,request对象是全局可用的,可以通过HttpServletRequest
类来获取。
1、获取请求头信息
String headerValue = request.getHeader("headerName");
2、获取请求参数
String paramValue = request.getParameter("paramName");
3、获取请求方法
String method = request.getMethod(); // "GET" or "POST"
4、设置请求属性
request.setAttribute("attributeName", attributeValue);
5、获取请求属性
Object attributeValue = request.getAttribute("attributeName");
6、获取请求URI
String requestURI = request.getRequestURI();
7、获取请求URL
StringBuffer requestURL = request.getRequestURL(); // includes protocol, server name, and port number
8、获取远程IP地址
String remoteAddr = request.getRemoteAddr(); // client's IP address from the remote host, e.g. "192.168.1.100"
9、获取HTTP协议版本
String protocolVersion = request.getProtocol(); // "HTTP/1.1" or "HTTP/1.0"
10、设置Cookie
Cookie cookie = new Cookie("cookieName", "cookieValue"); // set cookie with a specific domain and path for security reasons (optional) response.addCookie(cookie); // add the cookie to the response object (optional)