生活随笔
收集整理的這篇文章主要介紹了
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
[java]?view plaincopy
@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();??????????????????????????????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)?{????????????????????????????????????e.printStackTrace();??????????}??????}??
Global.java
[java]?view plaincopy
import?javax.servlet.http.HttpServletRequest;??import?javax.servlet.http.HttpSession;??????import?org.springframework.web.context.request.RequestContextHolder;??import?org.springframework.web.context.request.ServletRequestAttributes;???????????????public?class?Global?{??????static?String?_loginSessionKey="login_session";???????????????public?static?HttpServletRequest?getHttpServletRequest(){??????????HttpServletRequest?request?=?((ServletRequestAttributes)RequestContextHolder??????????????????.getRequestAttributes())??????????????????.getRequest();??????????return?request;??????}??????????????????public?static?HttpSession?getHttpSession(){??????????return?getHttpServletRequest().getSession();??????}??}??
web.xml 加入監聽
<!-- 5. spring 注冊監聽,獲取上下文中的 HttpServletRequest 對象(對 Global 支持) ?-->
[html]?view plaincopy
<listener>????????<listener-class>????????????org.springframework.web.context.request.RequestContextListener????????</listener-class>????</listener>???
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的spring几种获取 HttpServletRequest 对象的方式的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。