• 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 }

    访问如下所示:

  • 相关阅读:
    继承
    面向对象
    数据库的数据操作
    数据库数据类型以及建库语句
    第一天
    继承与多态
    C#面向对象——对象成员、方法重载、引用类库等
    C#面向对象初步
    SQL2008知识回顾
    C#知识回顾
  • 原文地址:https://www.cnblogs.com/biehongli/p/13872902.html
Copyright © 2020-2023  润新知