Springboot使用内置对象HttpServletRequest、HttpServletResponse

2020-10-30 10:37:18 浏览数 (1)

1、通过Springboot程序可以发现,Springboot中控制器的形式和Springmvc中是一样的,因此在程序中使用jsp的内置对象也可以按照与Springmvc同样的方式进行。

代码语言:javascript复制
 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形式来获取内置对象。

代码语言:javascript复制
 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 }

访问如下所示:

0 人点赞