Springboot使用内置对象HttpServletRequest、HttpServletResponse
1、通过Springboot程序可以发现,Springboot中控制器的形式和Springmvc中是一样的,因此在程序中使用jsp的内置对象也可以按照与Springmvc同样的方式进行。
1 package org.springboot.tentent.controller; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 import org.springframework.web.bind.annotation.RequestMapping; 10 import org.springframework.web.bind.annotation.RestController; 11 12 @RestController 13 public class SampleController { 14 15 @RequestMapping(value = "/hello") 16 public Map<String, String> hello(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { 17 Map<String, String> map = new HashMap<String, String>(); 18 map.put("客户端的ip地址: ", httpServletRequest.getRemoteAddr()); 19 map.put("客户端的响应编码: ", httpServletResponse.getCharacterEncoding()); 20 map.put("客户端的SessionID: ", httpServletRequest.getSession().getId()); 21 map.put("项目的真实路径: ", httpServletRequest.getServletContext().getRealPath("/")); 22 return map; 23 } 24 25 }
访问如下所示:
除了在控制器的方法上使用参数来接收内置对象外,也可以利用ServletRequestAttributes形式来获取内置对象。
1 package org.springboot.tentent.controller; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 import org.springframework.web.bind.annotation.RequestMapping; 10 import org.springframework.web.bind.annotation.RestController; 11 import org.springframework.web.context.request.RequestContextHolder; 12 import org.springframework.web.context.request.ServletRequestAttributes; 13 14 @RestController 15 public class SampleController { 16 17 @RequestMapping(value = "/hello") 18 public Map<String, String> hello(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { 19 Map<String, String> map = new HashMap<String, String>(); 20 map.put("客户端的ip地址: ", httpServletRequest.getRemoteAddr()); 21 map.put("客户端的响应编码: ", httpServletResponse.getCharacterEncoding()); 22 map.put("客户端的SessionID: ", httpServletRequest.getSession().getId()); 23 map.put("项目的真实路径: ", httpServletRequest.getServletContext().getRealPath("/")); 24 return map; 25 } 26 27 @RequestMapping(value = "/hello2") 28 public Map<String, String> hello() { 29 // 获取HttpServletRequest内置对象 30 HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) 31 .getRequest(); 32 // 获取HttpServletResponse内置对象 33 HttpServletResponse httpServletResponse = ((ServletRequestAttributes) RequestContextHolder 34 .getRequestAttributes()).getResponse(); 35 Map<String, String> map = new HashMap<String, String>(); 36 map.put("客户端的ip地址: ", httpServletRequest.getRemoteAddr()); 37 map.put("客户端的响应编码: ", httpServletResponse.getCharacterEncoding()); 38 map.put("客户端的SessionID: ", httpServletRequest.getSession().getId()); 39 map.put("项目的真实路径: ", httpServletRequest.getServletContext().getRealPath("/")); 40 return map; 41 } 42 43 }
访问如下所示:
分类:
SpringBoot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
2017-10-25 一脸懵逼学习HBase的搭建(注意HBase的版本)
2017-10-25 一脸懵逼学习HBase---基于HDFS实现的。(Hadoop的数据库,分布式的,大数据量的,随机的,实时的,非关系型数据库)