spring几种获取 HttpServletRequest 对象的方式
以下的 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 支持) -->
- <listener>
- <listener-class>
- org.springframework.web.context.request.RequestContextListener
- </listener-class>
- </listener>
https://blog.csdn.net/hellozhxy/article/details/80775832
spring几种获取 HttpServletRequest 对象的方式的更多相关文章
- spring mvc中几种获取request对象的方式
在使用spring进行web开发的时候,优势会用到request对象,用来获取访问ip.请求头信息等 这里收集几种获取request对象的方式 方法一:在controller里面的加参数 public ...
- 通过RequestContextHolder直接获取HttpServletRequest对象
问题 朋友遇到一个问题:他想在Service方法中使用HttpServletRequest的API,但是又不想把HttpServletRequest对象当作这个Service方法的参数传过来,原因是这 ...
- SpringMVC 02: SpringMVC响应get和post请求 + 5种获取前端数据的方式
响应get和post请求 SpringMVC中使用@RequestMapping注解完成对get请求和post请求的响应 项目结构和配置文件与SpringMVC博客集中的"SpringMVC ...
- Js之Dom学习-三种获取页面元素的方式、事件、innerText和innerHTML的异同
一.三种获取页面元素的方式: getElementById:通过id来获取 <body> <input type="text" value="请输入一个 ...
- 流式思想概述和两种获取Stream流的方式
流式思想概述 整体来看,流式思想类似于工厂车间的生产流水线 当需要对多个元素进行操作(特别是多步操作)的时候,考虑到性能及便利性,我们应该首先拼好一个模型步骤方案,然后再按照方法去执行他 这张图中展示 ...
- javascript一种新的对象创建方式-Object.create()
1.Object.create() 是什么? Object.create(proto [, propertiesObject ]) 是E5中提出的一种新的对象创建方式,第一个参数是要继承的原型,如果不 ...
- 转: .NET MVC3 几种返回 JSON 对象的方式和注意事项
.NET MVC3 几种返回 JSON 对象的方式和注意事项 转自:http://blog.csdn.net/xxj_jing/article/details/7382589 引言在用 .NET MV ...
- 阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
目前这里能调用是因为,在service的实现类里面,new了一个dao的对象 正常情况下 这里不应该是new一个对象,应该等于null或为空 设置为空侯再运行就会报错 出错的原因是这里为null 需要 ...
- spring mvc 中获取HttpServletRequest ,HttpServletResponse
spring中的bean最常用的 singleton 模式 如果要在springmvc Controller 中获取 HttpServletRequest ,HttpServletResponse ...
随机推荐
- 使用window10系统搭建完善的Linux开发环境
https://juejin.im/post/5d22e46ee51d45775746b9b1 导读 在使用window系统开发时由于系统环境和线上环境不一致可能导致各种问题,以及部分扩展库只支持li ...
- 【转】Linux iptables 详解
转自:https://www.cnblogs.com/qwertwwwe/p/9452370.html 最近搭一个框架需要用到iptables做映射,学习了下iptables的原理,总结下方便以后查~ ...
- 使用Spring MVC统一异常处理实战(转载)
原文地址:http://blog.csdn.net/ufo2910628/article/details/40399539 种方式: (1)使用Spring MVC提供的简单异常处理器SimpleMa ...
- ArrayList为什么是线程不安全的
首先需要了解什么是线程安全:线程安全就是说多线程访问同一代码(对象.变量等),不会产生不确定的结果. 既然说ArrayList是线程不安全的,那么在多线程中操作一个ArrayList对象,则会出现不确 ...
- vue 中一些API 或属性的常见用法
prop 官方解释:Prop 是你可以在组件上注册的一些自定义特性.当一个值传递给一个 prop 特性的时候,它就变成了那个组件实例的一个属性. 通俗点讲就是:prop是父组件用来传递数据的一个自定义 ...
- form表单和CSS
一.form表单 1. form表单有什么用 能够获取用户输入的信息(输入,选择, 上传的文件),并且将这些数据全部发送给后端 2. form表单的用法 (1)有两个重要参数: action : 控制 ...
- HDU-4185-Oil Skimming(最大匹配)
链接: https://vjudge.net/problem/HDU-4185 题意: Thanks to a certain "green" resources company, ...
- 对items函数的理解
老师:dict的items应该是把dict转成列表,每个列表元素是一个包含key ,value的dict,元素应该是元组, {a:1, b:2, c:3} [(a, 1), (b,2), (c, ...
- 参数上使用自定义注解在aop中无法获取到该参数
https://ask.csdn.net/questions/769477 /** * 环绕增强,验证权限 * @param joinPoint 目标对象 * @param authCheck 自定义 ...
- MySQL教程-MyISAM和InnoDB的区别
MySQL的表类型MyISAM和InnoDB之间的最大区别是,InnoDB的支持事务.兄弟连教育( )来给大家做个对比: InnoDB支持一些新的功能:交易,行级锁,外键 InnoDB是高容量,高性能 ...