Action访问Servlet API
访问Servlet API
1.通过ActionContent类访问Servlet API
ActionContext中访问Servlet API的几个常用的方法:
(1)Map getApplication():
返回模拟该应用的ServletContext实例,可以把这个Map实例就当做是ServletContext实例,首先它是Map同时他也是ServletContext实例;
(2)Map getParameters():
获取所有请求参数;
(3)Map getSession():
返回一个Map对象,该对象相当于HttpSession实例;
(4)setApplication(Map application):
传入一个Map实例,将该实例的key-value对转换成application属性中的属性名和属性值;
(5)setSession(Map session):
传入一个Map实例,将该实例的key-value对转换成session的属性名和值。
代码:
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Test_1Action extends ActionSupport{
//定义封装请求参数的成员变量
private String username;
private String password;
//setter、getter方法
public void setUsername(String username){
this.username = username;
}
public String getUsername(){
return this.username;
}
public void setPassword(String password){
this.password = password;
}
public String getPassword(){
return this.password;
}
//定义处理请求的execute方法
public String execute()throws Exception{
if(getUsername()!=null){
//使用ActionContext的静态方法getContext获取ActionContext的实例
ActionContext context = ActionContext.getContext();
//通过ActionContext实例设置application范围的属性
String name = (String)context.getApplication().get("name");
if(name==null){
//设置其为application范围的属性
context.getApplication().put("name", "jiagoushi_1");
}
//通过ActionContext设置session范围的属性
context.getSession().put("username", username);
//通过ActionContext设置request范围的属性
context.put("username", username);
context.put("password", password);
return SUCCESS;
}else{
return ERROR;
}
}
}
2.使用ServletActionContext工具类访问Servlet API
在ServletActionContext工具类中常用的几个方法(都是静态方法):
(1)PageContext getPageContext():
取得应用的PageContext对象;
(2)HttpServletRequest getRequest():
取得该应用的HttpServletRequest对象;
(3)HttpServletRequest getResponse():
取得该应用的HttpServletResponse对象;
(4)ServletContext getServletContext():
取得该应用的ServletContext对象。
代码:
import javax.servlet.http.Cookie;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Test_1Action extends ActionSupport{
//定义封装请求参数的成员变量
private String username;
private String password;
//setter、getter方法
public void setUsername(String username){
this.username = username;
}
public String getUsername(){
return this.username;
}
public void setPassword(String password){
this.password = password;
}
public String getPassword(){
return this.password;
}
//定义处理请求的execute方法
public String execute()throws Exception{
if(getUsername()!=null){
//通过ServletActionContext获取并设置request
ServletActionContext.getRequest().setAttribute("username", getUsername());
//通过ServletActionContext获取response并使用response添加Cookie
Cookie cookie = new Cookie("user", getUsername());
cookie.setMaxAge(60*60);
ServletActionContext.getResponse().addCookie(cookie);
return SUCCESS;
}else{
return ERROR;
}
}
}
3.Action直接访问Servlet API
使用接口能够直接访问到Servlet API:
(1)ServletContextAware:
实现此接口的Action可直接访问Web应用的ServletContext实例;
(2)ServletRequestAware:
实现此接口的Action可直接访问Web应用的HttpServletRequest实例;
(3)ServletResponseAware:
实现此接口的Action可直接访问Web应用的HttpServletResponset实例;
代码:
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
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.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
public class Test_1Action extends ActionSupport implements
ServletContextAware,ServletRequestAware,ServletResponseAware{
//定义封装请求参数的成员变量
private String username;
private String password;
//用到的Web应用的实例
private ServletContext application;
private HttpServletRequest request;
private HttpServletResponse response;
//setter、getter方法
public void setUsername(String username){
this.username = username;
}
public String getUsername(){
return this.username;
}
public void setPassword(String password){
this.password = password;
}
public String getPassword(){
return this.password;
}
//定义处理请求的execute方法
public String execute()throws Exception{
if(getUsername()!=null){
//直接使用Web应用的对象
//设置application范围的属性
if(application.getAttribute("user")==null){
application.setAttribute("user", "jiagoushi");
}
//设置request范围的属性
request.setAttribute("username", getUsername());
request.setAttribute("password", getPassword());
//设置response范围的属性,并添加Cookie
Cookie cookie = new Cookie("myCookie", getUsername());
cookie.setMaxAge(60*60);
response.addCookie(cookie);
return SUCCESS;
}else{
return ERROR;
}
}
//实现ServletContextAware接口必须实现的方法
@Override
public void setServletContext(ServletContext application) {
this.application = application;
}
//实现ServletRequestAware接口必须实现的方法
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
//实现ServletResponseAware接口必须实现的方法
@Override
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
}
Action访问Servlet API的更多相关文章
- struts2 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用
Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...
- struts2的action访问servlet API的三种方法
学IT技术,就是要学习... 今天无聊看看struts2,发现struts2的action访问servlet API的三种方法: 1.Struts2提供的ActionContext类 Object g ...
- Struts 2读书笔记-----Action访问Servlet API
Action访问Servlet API Struts2中的Action并没有和任何Servlet API耦合,这样框架更具灵活性,更易测试. 对于Web应用的控制器而言,不访问ServletAPI是几 ...
- Action访问Servlet API的三种方法
一.为什么要访问Servlet API ? Struts2的Action并未与Servlet API进行耦合,这是Struts2 的一个改良,从而方便了单独对Action进行测试.但是对于Web控制器 ...
- 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用
Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...
- Struts2笔记--Action访问Servlet API
Web应用中通常需要访问的Servlet API就是HttpServletRequest.HttpSession和ServletContext,这三个接口分别代表JSP内置对象中的request.se ...
- Struts2学习笔记(五)——Action访问Servlet API
在Strut2中访问Servlet API有三种方式: 1.通过ActionContext访问Servlet API,推荐使用这种,但是这种方案它获取的不是真正的事Servlet API. 步骤: 1 ...
- 关于Struts2自动装配和访问Servlet API
自动装配 1.根据属性的getter和setter获取值 index.jsp <s:form action="hello" method="POST"& ...
- Action访问Servlet的API
Action访问Servlet的API_,主要访问如下: 1.>获取request对象 2.>获取请求参数 3.>获取response对象,可用于传递cookie 3.>获取作 ...
随机推荐
- Android中实现两次点击返回键退出本程序
1,当用户使用我们的app的时候,有时候无意的或者不是有心的按下了我们的返回键,这时候为了更好的用体验,我们需要让用户再一次确定一下,以便判断用户的真实意图 代码如下: //该功能实现退出时提示的功能 ...
- paper 74:MATLAB图像处理_HSV与RGB颜色空间互转
HSV空间:分别是H(色调)——S(饱和度)——V(亮度) 与HSI颜色空间类似:分别是H(色调)——S(饱和度)——I(强度) 注意: 强度和亮度差不多是一个概念. 饱和度代表的是渗入白光的数量级, ...
- AMD机制与cMD的区别和概念简要介绍
1.http://www.cnblogs.com/dojo-lzz/p/4707725.html 2.http://blog.chinaunix.net/uid-26672038-id-4112229 ...
- UINavigationController(转)
UINavigationController是IOS编程中比较常用的一种容器view controller,很多系统的控件(如UIImagePickerViewController)以及很多有名的AP ...
- CSS的样式合并与模块化
by zhangxinxu from http://www.zhangxinxu.com 原文地址:http://www.zhangxinxu.com/wordpress/?p=931 一.引言 本文 ...
- PHPExcel读取excel文件示例
PHPExcel读取excel文件示例PHPExcel最新版官方下载网址:http://phpexcel.codeplex.com/PHPExcel是一个非常方便生成Excel格式文件的类,官方下载包 ...
- html 关于块级元素和行内元素
常用的行内元素要记住:a.span.img.input.lable.select.strong.textarea 常用的块级元素要记住:div.h1~h6.dl.ul.ol 例如在一个title中,有 ...
- 安装LAMP
1.首先打开命令行,获得最新的软件包 sudo apt-get install update 2.安装MySQL数据库 sudo apt-get install mysql-server mysql- ...
- linux 集群配置ssh无密码访问
一.修改host文件 1) 用客户端工具(ssh client或者putty)连接到linux服务器.在root用户下输入命令 vi /etc/hosts,用vi编辑hosts文件,如下: #127. ...
- SQL中的连接可以分为内连接,外连接,以及交叉连接 。
SQL中的连接可以分为内连接,外连接,以及交叉连接 . 1. 交叉连接CROSS JOIN 如果不带WHERE条件子句,它将会返回被连接的两个表的笛卡尔积,返回结果的行数等于两个表行数的乘积: 举例, ...