`

struts2源码分析获取request response session

阅读更多
网上介绍有两种获取request,response和session的方法,一种是ioc方式的,通过实现SessionAware,ServletRequestAware, ServletResponseAware接口就可以
而另一种则是非ioc方式的,我这主要介绍一下非ioc方式的
1.获取request
HttpServletRequest req1 = ServletActionContext.getRequest();
HttpServletRequest req2= (HttpServletRequest)ActionContext.getContext().get(ServletActionContext. HTTP_REQUEST ); 		
HttpServletRequest req4= (HttpServletRequest)ActionContext.getContext().get(StrutsStatics. HTTP_REQUEST ); 	
HttpServletRequest req5= (HttpServletRequest)ActionContext.getContext().get("com.opensymphony.xwork2.dispatcher.HttpServletRequest"); 

其实它们最终是走的一个方法,因为ServletActionContext 继承了ActionContext
而且因为ServletActionContext.getRequest()的方法是调用了ActionContext里的方法
同时ServletActionContext. HTTP_REQUEST,StrutsStatics. HTTP_REQUEST常量都是字符串com.opensymphony.xwork2.dispatcher.HttpServletRequest,
它的定义在StrutsStatics接口中,ServletActionContext也继承了接接口,所有也能直接使用这个常量

2.同理获取response
HttpServletResponse res1 = ServletActionContext.getResponse();
HttpServletResponse res2= (HttpServletResponse)ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE); 		
HttpServletResponse res3= (HttpServletResponse)ActionContext.getContext().get(StrutsStatics.HTTP_RESPONSE ); 	
HttpServletResponse res4= (HttpServletResponse)ActionContext.getContext().get("com.opensymphony.xwork2.dispatcher.HttpServletResponse"); 

3.获取session 只需要先获取到request就可以通过request.getSession()来获取
0
1
分享到:
评论

相关推荐

    Struts2的Action中获得request response session几种方法

    Struts2的Action中获得request response session几种方法

    struts2创建 request response session application

    一个Demo告诉你struts2如何创建 request response session application

    struts2使用request、response

    struts1中使用request、response都是方法自带的,而struts2中的request、response、session都被隐藏的了,该带代码文档会清晰的让你明白struts2中request、response在自定义方法中如何随意使用

    STRUTS2获得作用域、参数响应对象及三种符号说明

    RequestMap,SessionMap,ApplicationMap, AttributeMap. struts提供两种方式访问session,request ,response,ActionContext 使用。 获得作用域及参数响应对象及三种符号说明

    POI导入导出EXCEL文件.(struts 1.x and struts2.x).doc

    struts1.x的例子,struts2.x可以参考自己修改 1.action的写法 import java.io.*; import java.sql.*; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import javax.servlet....

    struts2的学习笔记+测试源代码

    如何获取request,session,response对象 博文链接:https://wuzhaohuixy-qq-com.iteye.com/blog/710102

    从J2SE到J2EE知识点介绍

    (五) jsp内置对象(request,response.session,out) 121 1. request和response 121 2. session 122 (六) Servlet的 xml配置 126 1. xml的文件结构 127 2. 通过web.xml配置文件访问servlet的流程 129 (七) servlet里面...

    django框架实现模板中获取request 的各种信息示例

    本文实例讲述了django框架实现模板中获取request 的各种信息。分享给大家供大家参考,具体如下: 在做网页程序时,request,response 是少不了的,这是最基本的东西。但 django有自己的框架,在展示层,很少用到直接...

    BaseAction

    在Struts2中,操作Servlet API 的BaseAction 包括获取Request对象、获取Session对象、获取ServletContext对象、获取Response对象

    Java Web编程宝典-十年典藏版.pdf.part2(共2个)

    8.3.1 Struts2的获取与放置 8.3.2 第一个Struts2程序 8.4 Action对象 8.4.1 认识Action对象 8.4.2 请求参数的注入原理 8.4.3 Action的基本流程 8.4.4 什么是动态Action 8.4.5 动态Action的应用 8.5 Struts2的配置...

    struts 标签 logic:iterate使用 logic:iterate

    ActionForm form, HttpServletRequest request, HttpServletResponse response) { InformationForm informationForm = (InformationForm) form;// TODO Auto-generated method stub //业务开始 ...

    拦截器和控制器的区别

    1、struts2对servlet封装(request,response) ,资源调配和资源的映射 2、框架设计的思想 istruts 配置,过滤器,反射 istruts.properties 3、starts2的使用思路 1、jar 2、配置文件 3、常用类 ...

    外文翻译 stus MVC

    For instance, the Struts framework will take fname from request stream and call UserActionForm.setFname(). • The Struts framework updates the state of the UserActionForm before passing it to the ...

    spring_MVC源码

    弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...

    Servlets和JSP核心技术 卷2(英文版) 第二部分

    Servlets和JSP核心技术 卷2 内容还是很详细的,看过卷1的人可以继续用这本书深造,呵呵 目录: Chapter 1. Using and Deploying Web Applications Section 1.1. Purpose of Web Applications Section 1.2. ...

    Servlets和JSP核心技术 卷2(英文版) 第一部分

    Servlets和JSP核心技术 卷2 内容还是很详细的,看过卷1的人可以继续用这本书深造,呵呵 目录: Chapter 1. Using and Deploying Web Applications Section 1.1. Purpose of Web Applications Section 1.2. ...

    李兴华 Java Web 开发实战经典_带源码_高清pdf 带书签 上

    6.4、response对象 6.4.1、设置头信息 6.4.2、页面跳转 6.4.3、操作Cookie 6.5、session对象 6.5.1、取得Session Id 6.5.2、登陆及注销 6.5.3、判断新用户 6.5.4、取得用户的操作时间 6.6、application对象...

    J2EE面试题

    2) 利用JavaMail API发送邮件的过程中,()能获取正确的session对象。 a) Session mailsession=Session.getInstance(); b) Session mailsession=Session.getInstance(objProperties); c) Session mailsession...

    java web 视频、电子书、源码(李兴华老师出版)

    6.4、response对象 6.4.1、设置头信息 6.4.2、页面跳转 6.4.3、操作Cookie 6.5、session对象 6.5.1、取得Session Id 6.5.2、登陆及注销 6.5.3、判断新用户 6.5.4、取得用户的操作时间 6.6、...

Global site tag (gtag.js) - Google Analytics