以下的 request 实例都编号了,一共 4 种 方式

1.@Autowired 方式
2.public void Test(HttpServletRequest request1, HttpServletResponse resp,HttpSession session1) 方式
3.((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); 方式
4.Global.getHttpServletRequest(); 方式,该方式基于 第三种做的封装

方法4 最灵活,不需要每个 action 中都定义 HttpServletRequest 参数。

LoginController.java

@Controller
@RequestMapping("/demo")
public class LoginController{
@Autowired
private HttpServletRequest request2; @RequestMapping("test")
@ResponseBody
public void Test(HttpServletRequest request1, HttpServletResponse resp,HttpSession session1){
resp.setCharacterEncoding("UTF=8");
resp.setContentType("text/html;charset=UTF-8"); String key = "test_date";
HttpSession session = request1.getSession();
session.setAttribute(key, new Date());
String sessionId = session.getId();
Date time = (Date)session.getAttribute(key); HttpServletRequest request3 = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
HttpServletRequest request4 = Global.getHttpServletRequest(); //判断session是不是新创建的
try {
if (session.isNew()) {
resp.getWriter().print(java.text.MessageFormat.format(
"【{0}】session创建成功,session的id是:</br>{1}</br>{2}</br>{3}</br>{4}</br>{5}"
, StringExtend.getString(time)
, sessionId
, session1.getId()
, request2.getSession().getId()
, request3.getSession().getId()
, request4.getSession().getId())); }else {
resp.getWriter().print(java.text.MessageFormat.format(
"【{0}】服务器已经存在该session了,session的id是:</br>{1}</br>{2}</br>{3}</br>{4}</br>{5}"
, StringExtend.getString(time)
, sessionId
, session1.getId()
, request2.getSession().getId()
, request3.getSession().getId()
, request4.getSession().getId()));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Global.java

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; /**
* 使用该类必须在 web.xml 中添加监听(org.springframework.web.context.request.RequestContextListener)
* 该作用域仅适用于WebApplicationContext环境
*/
public class Global {
static String _loginSessionKey="login_session";
/**
* 获取当前请求session
* @return
*/
public static HttpServletRequest getHttpServletRequest(){
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder
.getRequestAttributes())
.getRequest();
return request;
}
/**
* 获取当前请求session
* @return
*/
public static HttpSession getHttpSession(){
return getHttpServletRequest().getSession();
}
}

web.xml 加入监听

<!-- 5. spring 注册监听,获取上下文中的 HttpServletRequest 对象(对 Global 支持)  -->

  1. <listener>
  2. <listener-class>
  3. org.springframework.web.context.request.RequestContextListener
  4. </listener-class>
  5. </listener>

https://blog.csdn.net/hellozhxy/article/details/80775832

spring几种获取 HttpServletRequest 对象的方式的更多相关文章

  1. spring mvc中几种获取request对象的方式

    在使用spring进行web开发的时候,优势会用到request对象,用来获取访问ip.请求头信息等 这里收集几种获取request对象的方式 方法一:在controller里面的加参数 public ...

  2. 通过RequestContextHolder直接获取HttpServletRequest对象

    问题 朋友遇到一个问题:他想在Service方法中使用HttpServletRequest的API,但是又不想把HttpServletRequest对象当作这个Service方法的参数传过来,原因是这 ...

  3. SpringMVC 02: SpringMVC响应get和post请求 + 5种获取前端数据的方式

    响应get和post请求 SpringMVC中使用@RequestMapping注解完成对get请求和post请求的响应 项目结构和配置文件与SpringMVC博客集中的"SpringMVC ...

  4. Js之Dom学习-三种获取页面元素的方式、事件、innerText和innerHTML的异同

    一.三种获取页面元素的方式: getElementById:通过id来获取 <body> <input type="text" value="请输入一个 ...

  5. 流式思想概述和两种获取Stream流的方式

    流式思想概述 整体来看,流式思想类似于工厂车间的生产流水线 当需要对多个元素进行操作(特别是多步操作)的时候,考虑到性能及便利性,我们应该首先拼好一个模型步骤方案,然后再按照方法去执行他 这张图中展示 ...

  6. javascript一种新的对象创建方式-Object.create()

    1.Object.create() 是什么? Object.create(proto [, propertiesObject ]) 是E5中提出的一种新的对象创建方式,第一个参数是要继承的原型,如果不 ...

  7. 转: .NET MVC3 几种返回 JSON 对象的方式和注意事项

    .NET MVC3 几种返回 JSON 对象的方式和注意事项 转自:http://blog.csdn.net/xxj_jing/article/details/7382589 引言在用 .NET MV ...

  8. 阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式

    目前这里能调用是因为,在service的实现类里面,new了一个dao的对象 正常情况下 这里不应该是new一个对象,应该等于null或为空 设置为空侯再运行就会报错 出错的原因是这里为null 需要 ...

  9. spring mvc 中获取HttpServletRequest ,HttpServletResponse

    spring中的bean最常用的 singleton 模式 如果要在springmvc Controller 中获取  HttpServletRequest ,HttpServletResponse ...

随机推荐

  1. 【原】iptables 交叉编译

    防火墙在做数据包过滤决定时,有一套遵循和组成的规则,这些规则存储在专用的数据包过滤表中,而这些表集成在 Linux 内核中.在数据包过滤表中,规则被分组放在我们所谓的链(chain)中.而netfil ...

  2. 牛客练习赛26B 烟花 (概率DP)

    链接:https://ac.nowcoder.com/acm/contest/180/B 来源:牛客网 烟花 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5 ...

  3. RPC框架pigeon源码分析

    Pigeon是一个分布式服务通信框架(RPC),是美团点评最基础的底层框架之一.已开源,链接:https://github.com/dianping/pigeon 从接下来三个方面来分析pigeon的 ...

  4. 如何使用hook(非注入dll)

    #include <Windows.h> #include <iostream> using namespace std; HHOOK mouseHook; LRESULT _ ...

  5. Notepad++技巧

    1 正则表达式的查找和替换,例如删除每行开始的数字ctrl+H, ^\d\d\d:null 2 删除所有的空行: TextFX插件->Edit->Delete Blank Lines 3 ...

  6. java8中 map和flatmap的理解

    假如我们有这样一个需求给定单词列表["Hello","World"],你想要返回列表["H","e","l&q ...

  7. linux 环境下安装MySQL5.7(yum)

    安装环境: CentOS7 64位,MySQL5.7 原文链接:https://blog.csdn.net/xyang81/article/details/51759200 1. 配置yum源 在My ...

  8. day_05 if条件判断和while循环作业题

    1. 输入姑娘的年龄后,进行以下判断: 1. 如果姑娘小于18岁,打印“不接受未成年” 2. 如果姑娘大于18岁小于25岁,打印“心动表白” 3. 如果姑娘大于25岁小于45岁,打印“阿姨好” 4. ...

  9. 解决“Caused by: org.gradle.api.plugins.UnknownPluginException: Plugin with id 'org.springframework.boot' not found.”

    升级老项目spring boot 和 cloud版本之后 gradle clean 报错:“Caused by: org.gradle.api.plugins.UnknownPluginExcepti ...

  10. 《Spring源码深度解析》二

    第二章:容器的基本实现 2.1 基本用法 首先定义一个Bean:(假设在bean包下) 然后定义配置文件: 测试类: 当然,这并非是企业级用法,此处只是用来分析学习其实现 2.2 功能分析 上述三图代 ...