以下的 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. pause模块

    pause模块:暂停脚本执行 # ansible-doc -s pause- name: Pause playbook execution pause: minutes: // 暂停的真实分钟数 pr ...

  2. static静态和非静态详解

    static 作为Java中的一个关键字,用于修饰方法.成员变量(Field),统称为成员. 有static修饰的成员   属于类 1.方法称为静态方法(类方法),Field称为类的属性. 2.静态成 ...

  3. Codeforces 903 绝对值1e19longdouble算贡献 汉明距离交换两项是否可行

    A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...

  4. TypeError: Cannot read property 'splice' of undefined

    splice是删除数组里的项,报这个错证明你点前面那个并不是个数组,仔细一看,还真是数组名称写错了

  5. windows中ftp下载脚本(bat+vb)

    做了个ftp下载脚本: ftpdownload.bat @rem 注释:从ftp服务器每小时下载北向性能文件的脚本 @rem 用vb脚本取昨天 for /f %%a in ('cscript //no ...

  6. 那些年我写过的mysql命令

    建表语句 #mysql5.7适用create table testfy ( id int primary key AUTO_INCREMENT, clipid int comment '影片编号', ...

  7. GDOI2017总结

    前言 大概在两个星期前,由于会有一堆人因为限人数的问题而被卡掉,当时那个人心惶惶啊,搞到我们心惊胆战,茶饭不安. 话说某日,jacky36当众表示,辣鸡余可灿,把我卡掉啦,B~(屏蔽不良言语).余可灿 ...

  8. HTML5测试(一)

    HTML5测试一 1. 问题:HTML5 之前的 HTML 版本是什么? A.HTML 4.01 B.HTML 4 C.HTML 4.1 D.HTML 4.9 答案:A HTML5 是 HTML 最新 ...

  9. 代码检测docker-sonarqube

    gitlab-ce + gitlab-runner + sonarqube,在提交代码时对代码质量进行检测,对不符合要求的代码不允许提交到gitlab version: '3.1' services: ...

  10. AJAX 实例解析

    AJAX 实例 为了帮助您理解 AJAX 的工作原理,我们创建了一个小型的 AJAX 应用程序: 实例 AJAX 不是新的编程语言,而是一种使用现有标准的新方法.深圳dd马达 AJAX 是与服务器交换 ...