以下的 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. [转载]Ethernet,Half-Duplex/Full-Duplex,CSMA

    原文地址:Ethernet,Half-Duplex/Full-Duplex,CSMA/CD,Auto-Negotiation作者:心田麦浪 CSMA/CD(Carrier Sense Multiple ...

  2. nginx web优化

    一  隐藏版本号 在nginx配置文件http里面添加: server_tokens off; 二  设置nginx默认登入用户   nginx编译安装的时候 事先创一个不能登入系统的普通用户 使用普 ...

  3. vue修改富文本中的元素样式

    富文本编辑器目前应用很广泛,而有时候我们想要对其中的一些元素的样式进行修改,就会遇到问题. 首先,直接修改是不可行的,因为是用v-html标签进行渲染的,无法直接获取到. 在修改的时候,一般是按标签进 ...

  4. [project X] tiny210(s5pv210)上电启动流程(BL0-BL2)(转)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/ooonebook/article/det ...

  5. maven 内置变量对应目录

    [b]在创建Maven工程后,插件配置中通常会用到一些Maven变量,因此需要找个地方对这些变量进行统一定义,下面介绍如何定义自定义变量.[/b] 在根节点project下增加properties节点 ...

  6. 【洛谷P2447】外星千足虫

    题目大意:给定一个 M 个含 N 个未知数的异或方程组,保证有解,若存在唯一解,给出至少需要几个方程才能得出唯一解,若不存在,直接输出不存在. 题解:异或方程组也满足类似初等行变换的操作,只不过所有的 ...

  7. mvn clean 后 mvn package失败

    项目完全没有问题,就是mvn clean package 失败,或者打包成功后发布项目无法启动. 很可能的原因是,clean后一些需要编译生成的文件还没有生成,直接package会出错,或者packa ...

  8. ZROI 19.08.09模拟赛

    传送门 写在前面:为了保护正睿题目版权,这里不放题面,只写题解. A \(70pts:\) 维护一个栈,从一侧向另一侧扫描,如果新加入的元素与当前栈顶相同,则出栈,否则进栈.显然一个子串是括号序列,当 ...

  9. Django ckeditor增加编辑代码 功能

    前言 使用ckeditor这个组件的时候 对于长写博客的同学当然希望能有 增加代码这个功能按钮 而这个按钮 需要自己配置 我们的编辑器自然需要添加代码块的功能. 需要用到插件codesnippet,c ...

  10. UVA-10480-Sabotage(最大流最小割,打印路径)

    链接: https://vjudge.net/problem/UVA-10480 题意: The regime of a small but wealthy dictatorship has been ...