随笔 - 597  文章 - 4  评论 - 445  阅读 - 424万

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 }
复制代码

访问如下所示:

 

posted on   别先生  阅读(3723)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 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的数据库,分布式的,大数据量的,随机的,实时的,非关系型数据库)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示