RequestAttributes , ServletRequestAttributes学习
publicstaticServletRequestAttributesgetRequestAttributes(){RequestAttributesattributesRequestContextHolder.getRequestAttributes();return(ServletRequestAttributes)attributes;}是 获取当前 HTTP 请求上下文的标准两步操作RequestAttributesattributesRequestContextHolder.getRequestAttributes();作用从 Spring 的上下文基于 ThreadLocal中获取当前线程绑定的请求属性对象。返回类型RequestAttributes这是一个 接口在 Web 环境如 Spring MVC 处理 HTTP 请求时→ 实际是 ServletRequestAttributes 实例原理Spring 在处理每个 HTTP 请求时通过 DispatcherServlet会创建 ServletRequestAttributes 对象包装 HttpServletRequest 和 HttpServletResponse调用 RequestContextHolder.setRequestAttributes(…) 将其存入 ThreadLocal请求结束后自动清理因此在同一个请求线程中任何地方调用 getRequestAttributes() 都能拿到这个对象。「接口-实现类」关系RequestAttributes 和 ServletRequestAttributes 是典型的「接口-实现类」关系也就是你所说的“父子关系”更准确地说是 接口与其实现类的关系// 定义接口interfaceAnimal{voidmakeSound();}// 实现类classDogimplementsAnimal{publicvoidmakeSound(){System.out.println(汪汪);}}// 使用AnimalmyPetnewDog();// ✅ 合法把 Dog 对象赋值给 Animal 接口变量myPet.makeSound();// 输出汪汪问题来了,为什么要强制转换一下呢Animal 这个例子里面 是因为 接口定义了makeSound 方法, 所以可以直接使用但如果方法里没有实现呢? 类在实现的时候新增的方法,就需要强制转换,才可以使用说法是否正确“可以把对象赋值给它的接口”✅ 完全正确“接口变量持有对象的实际类型信息”✅ 是的运行时保留“通过接口变量能调用子类所有方法”❌ 只能调用接口中声明的方法“这是 Java 多态的基础”✅ 正确ServletRequestAttributespublicclassServletRequestAttributesimplementsRequestAttributes,Serializable// 获取 HttpServletRequestpublic HttpServletRequest getRequest()// 获取 HttpServletResponsepublic HttpServletResponse getResponse()// 获取 HttpSession可选择是否创建public HttpSession getSession(boolean create)// 获取原生的 ServletContextpublic ServletContext getServletContext()// 1. 获取当前请求上下文ServletRequestAttributesattributes(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();// 2. 获取原始请求/响应对象HttpServletRequestrequestattributes.getRequest();HttpServletResponseresponseattributes.getResponse();// 3. 操作请求属性attributes.setAttribute(userId,123,RequestAttributes.SCOPE_REQUEST);StringuserId(String)attributes.getAttribute(userId,RequestAttributes.SCOPE_REQUEST);// 4. 操作会话属性attributes.setAttribute(cart,shoppingCart,RequestAttributes.SCOPE_SESSION);Objectcartattributes.getAttribute(cart,RequestAttributes.SCOPE_SESSION);// 5. 获取会话HttpSessionsessionattributes.getSession(true);HttpServletRequest request 的方法1.获取请求行信息Request Line// 获取 HTTP 方法GET、POST、PUT、DELETE 等StringgetMethod()// 获取请求的 URI不包含协议、主机、端口StringgetRequestURI()// 获取完整的请求 URL包含协议、主机、端口、URIStringBuffergetRequestURL()// 获取查询字符串URL 中 ? 后面的部分StringgetQueryString()// 获取协议及版本如 HTTP/1.1StringgetProtocol()// 获取上下文路径应用的部署路径以 / 开头StringgetContextPath()// 获取 Servlet 路径映射到当前 Servlet 的路径StringgetServletPath()2.获取请求头信息Request Headers// 获取指定请求头的值单个值StringgetHeader(Stringname)// 获取指定请求头的所有值多个值的情况EnumerationStringgetHeaders(Stringname)// 获取所有请求头的名称EnumerationStringgetHeaderNames()// 常用的特定请求头方法StringgetContentType()// Content-TypeintgetContentLength()// Content-LengthlonggetContentLengthLong()// Content-Length (long 类型)StringgetCharacterEncoding()// 字符编码LocalegetLocale()// 客户端首选语言EnumerationLocalegetLocales()// 所有支持的语言获取请求参数Request Parameters// 获取单个参数值StringgetParameter(Stringname)// 获取参数的所有值用于多选框等String[]getParameterValues(Stringname)// 获取所有参数名称EnumerationStringgetParameterNames()// 获取所有参数的 Map参数名 → 参数值数组MapString,String[]getParameterMap()4.获取请求体数据Request Body// 获取输入流用于读取原始请求体ServletInputStreamgetInputStream()// 获取字符阅读器用于读取文本请求体BufferedReadergetReader()5.获取会话信息Session// 获取当前会话如果不存在则创建HttpSessiongetSession()// 获取当前会话可选择是否创建HttpSessiongetSession(booleancreate)// 判断是否已有会话booleanisRequestedSessionIdValid()booleanisRequestedSessionIdFromCookie()booleanisRequestedSessionIdFromURL()6.获取客户端和服务器信息// 客户端信息StringgetRemoteAddr()// 客户端 IP 地址StringgetRemoteHost()// 客户端主机名intgetRemotePort()// 客户端端口// 服务器信息StringgetLocalAddr()// 服务器 IP 地址StringgetLocalName()// 服务器主机名intgetLocalPort()// 服务器端口StringgetServerName()// 服务器主机名intgetServerPort()// 服务器端口// 转发/包含信息StringgetRealPath(Stringpath)// 获取真实文件系统路径已废弃ServletContextgetServletContext()// 获取 ServletContext7.属性操作Attributes// 设置请求属性在请求范围内共享数据voidsetAttribute(Stringname,Objectobject)// 获取请求属性ObjectgetAttribute(Stringname)// 移除请求属性voidremoveAttribute(Stringname)// 获取所有属性名称EnumerationStringgetAttributeNames()8.其他重要方法// 获取认证信息StringgetAuthType()booleanisSecure()// 是否使用 HTTPSPrincipalgetUserPrincipal()booleanisUserInRole(Stringrole)// 获取 Cookie 信息Cookie[]getCookies()// 获取请求调度器RequestDispatchergetRequestDispatcher(Stringpath)// 获取字符编码voidsetCharacterEncoding(Stringenv)
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2528121.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!