zoukankan      html  css  js  c++  java
  • 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 }

    访问如下所示:

  • 相关阅读:
    Bootstrap_让Bootstrap轮播插件carousel支持左右滑动手势的三种方法
    JAVA_用Java来获取访问者真实的IP地址
    Html5_移动前端不得不了解的html5 head 头标签
    ThinkPad_T430重装系统
    JavaScript_JS判断客户端是否是iOS或者Android
    Html5_禁止Html5在手机上屏幕页面缩放
    HttpClient_httpclient 4.3.1 post get的工具类
    HttpClient_使用httpclient必须知道的参数设置及代码写法、存在的风险
    LATEX数学公式基本语法
    为WLW开发Latex公式插件
  • 原文地址:https://www.cnblogs.com/biehongli/p/13872902.html
Copyright © 2011-2022 走看看