在正式讲解如何获取上述对象之前,需要先搞清楚一点,类似于Struts2、SpringMVC框架之所以在诸多方面用着比较方便,简化开发人员重复机械性的工作,就是因为它们把底层的Servlet操作封装起来,替开发人员干了他们该干的工作,因此每一种框架都提供了获取底层Servlet的方式,好的,正式开始讲解本篇博客的内容;

首先讲解一种通过实现Struts2的某些个接口,来获取HttpServletRequest、HttpServletResponse、HttpSession的方式,先看如下几个接口源码:

import java.util.Map;/*** Actions that want access to the user's HTTP session attributes should implement this interface.<p>* <p/>* This will give them access to a Map where they can put objects that can be made available* to subsequent requests.<p/>* <p/>* Typical uses may be cached user data such as name, or a shopping cart.**/
public interface SessionAware {/*** Sets the Map of session attributes in the implementing class.** @param session a Map of HTTP session attribute name/value pairs.*/public void setSession(Map<String,Object> session);
}
package org.apache.struts2.interceptor;import javax.servlet.http.HttpServletRequest;/*** All Actions that want to have access to the servlet request object must implement this interface.<p>* <p/>* This interface is only relevant if the Action is used in a servlet environment. <p>* <p/>* Note that using this interface makes the Action tied to a servlet environment, so it should be* avoided if possible since things like unit testing will become more difficult.**/
public interface ServletRequestAware {/*** Sets the HTTP request object in implementing classes.** @param request the HTTP request.*/public void setServletRequest(HttpServletRequest request);
}
package org.apache.struts2.interceptor;import javax.servlet.http.HttpServletResponse;/*** All Actions that want to have access to the servlet response object must implement this interface.<p>* <p/>* This interface is only relevant if the Action is used in a servlet environment.<p>* <p/>* Note that using this interface makes the Action tied to a servlet environment, so it should be* avoided if possible since things like unit testing will become more difficult.**/
public interface ServletResponseAware {/*** Sets the HTTP response object in implementing classes.** @param response the HTTP response.*/public void setServletResponse(HttpServletResponse response);
}

上面这三个接口的大体作用就是如果Actions类想要访问Servlet请求、响应、Session对象,就需要依次实现这几个接口,且这三个接口只有处于Servlet环境中是才起作用,下面通过一个Action类,来演示如何获取 Servlet请求、响应、Session对象

package com.yxd.struts2.action;import java.util.Map;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;import com.opensymphony.xwork2.ActionSupport;public class GetServletDomainAction extends ActionSupport implementsSessionAware, ServletRequestAware, ServletResponseAware {private HttpServletResponse response;private HttpServletRequest request;private Map<String, Object> session;@Overridepublic void setServletResponse(HttpServletResponse response) {this.response = response;}@Overridepublic void setServletRequest(HttpServletRequest request) {this.request = request;}@Overridepublic void setSession(Map<String, Object> session) {this.session = session;}@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubreturn super.execute();}}

Struts2框架在实例化GetServletDomainAction对象是,会自动调用各种setter方法,将HttpServletRequest、HttpServletResponse、HttpSession对象设置进去,然后就可以在execute方法里面随便使用了;

第二种方式:Struts2中提供了ActionContext类用来获取Session对象中的属性值,同样提供了ServletActionContext类继承自ActionContext,用来获取HttpServletRequest、HttpServletResponse对象,下面看Action实例:

package com.yxd.struts2.action;import java.util.Map;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;public class GetServletDomainAction extends ActionSupport{@Overridepublic String execute() throws Exception {// 获取Session对象中属性的Map对象Map<String,Object> attrs = ActionContext.getContext().getSession();// 获取HttpServletRequest对象HttpServletRequest request = ServletActionContext.getRequest();// 获取HttpServletResponse对象HttpServletResponse response = ServletActionContext.getResponse();// TODO Auto-generated method stubreturn super.execute();}}

上述两种方式,是在Struts2项目中常用到的获取Servlet对象的方式,需要特别对获取的Session说明一下,大家会发现这个Session对象并不是真正意义上我们想要的HttpSession对象,获取到的是一个封装了HttpSession对象中各种属性的Map集合,那么如何根据获取到的这个Map销毁Session呢?

实际上我们通过上述两种方法获取到的Session对象,是一个org.apache.struts2.dispatcher.SessionMap<K, V>类型的一个实例,且该接口里面有invalidate()方法就是用来销毁Http Session对象的,因此可以通过将获取到的Map向下强制类型转换成SessionMap类型,然后调用这个方法即可,如下:

// 获取Session对象中属性的Map对象
Map<String,Object> attrs = ActionContext.getContext().getSession();// 销毁Session对象
if(attrs instanceof SessionMap){((SessionMap<String,Object>)attrs).invalidate();
}

最后在说一下如何获取传入到Action中的请求参数吧,跟上述两种方式差不多,可以直接通过ActionContext实例获取请求参数,也可以通过让Action类实现ParameterAware接口,然后再Action方法中通过调用setParameters(Map<String, String[]> parameters)将请求参数传递进Action里面去

Map<String,Object> parameters = ActionContext.getContext().getParameters();

上述两种方式都采用线程安全的方式获取Servlet对象,总体来说,在实际项目应用中足够使用了,Struts2官方推荐使用实现接口的方式获取Servlet对象,所以在日常工作中也建议使用该方式!

Struts2之HttpServletRequest、HttpServletResponse,HttpSession,Parameters处理相关推荐

  1. Struts2中访问HttpServletRequest和HttpSession

    2019独角兽企业重金招聘Python工程师标准>>> 关键字: struts2 httpservletrequest httpsession 在没有使用Struts2之前,都习惯使 ...

  2. The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes

    2019-04-15 16:30:21 org.apache.catalina.core.ApplicationDispatcher invoke 严重: Servlet.service() for ...

  3. Servlet 3 HttpServletRequest HttpServletResponse 验证码图片 form表单

    目录: HttpServletRequest: 获得请求行 和 客户机信息 获得请求头中referer信息,防止盗链 获得form提交数据 请求重定向 转发 RequestDispatcher.inc ...

  4. HttpServletRequest HttpServletResponse ServletException 重新打开后报红解决方法

    tomcat安装路径下\lib\servlet-api.jar 复制到Dynamic Web Project 的 WEB-INF/lib下,刷新 转载于:https://www.cnblogs.com ...

  5. Struts2中获取HttpServletRequest,HttpSession等的几种方式

    转自:http://www.kaifajie.cn/struts/8944.html package com.log;import java.io.IOException; import java.u ...

  6. 深入分析JavaWeb 44 -- Struts2开发核心之动作类Action

    一.Action动作类(一般用**Action结尾) struts2 的Action可以是POJO(Plain Old Java Object) 为了让用户开发的Action更加规范struts2提供 ...

  7. 深入分析JavaWeb Item44 -- Struts2开发核心之动作类Action

    一.Action动作类(一般用**Action结尾) struts2 的Action可以是POJO(Plain Old Java Object) 为了让用户开发的Action更加规范struts2提供 ...

  8. Spring MVC(一)

    Spring MVC 一 Spring MVC应用 附 mvc web项目搭建 第⼀部分 Spring MVC 应⽤ 第 1 节 Spring MVC 简介 1.1 MVC 体系结构 1.2 Spri ...

  9. SpringMVC基础配置及使用

    SpringMVC基础配置及使用 SpringMVC: 1.SpringMVC和Spring的关系:     软件开发的三层架构: web层[表示层.表现层]---->Service层----& ...

最新文章

  1. nginx环境的搭建
  2. 《科学》杂志做了一个清单,告诉你今年 10 个最重要的科技突破
  3. OSPF多区域;特殊区域;
  4. 《iOS取证实战:调查、分析与移动安全》一2.4 安全
  5. 搜集的一些项目源码,改改就能用
  6. SQL优化(二) 快速计算Distinct Count
  7. 1.5 引入解释性变量
  8. Java代码生成同一色系颜色_求大侠帮忙给这段JAVA代码 设置个背景颜色!
  9. ReactiveCocoa 更优雅的编程(信号探秘)
  10. 顺序容器之vector
  11. TensorFlow 2.x GPU版在conda虚拟环境下安装步骤
  12. android xml java混合编程_Android | 自动调整文本大小的 TextViews
  13. malloc 初始化_glibc: malloc、calloc、realloc amp; free
  14. qq说说时间轴php实现,QQ说说时间 qq说说时间轴
  15. arm板linux内核移植,ARM开发板上uClinux内核移植
  16. 怎样将kux格式转换mp4?高效简单的技巧你要懂
  17. 网络分布视频技术与盈利性视频站点技术
  18. FPS游戏自瞄爆头原理
  19. Windows Server 2008 R2 安装SP1补丁出错(0x800f0818)
  20. aptget本地源 ubuntu_apt-get Ubuntu本地ISO镜像入源

热门文章

  1. 【DP】UVA 103 Stacking Boxes 输出路径
  2. JavaScript 音频插件和图表插件
  3. css3 中background的新增加的属性的用法(一)
  4. 用css3和jQuery制作精美的表单
  5. CSS基础「五」定位
  6. 入门机器学习(八)--神经网络参数的反向传播算法
  7. 7-237 有理数加法 (15 分)
  8. 2021高考理综单科成绩查询,2021全国各省市高考总分及各科分数 分值是多少
  9. 操作系统用户态内核态线程同步
  10. JAVA实现置换加密和幻方加密(密码学)