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.>获取作 ...
随机推荐
- 记在centos中连接无线网络的一次过程
1. 首先, 你的系统要能驱动无限网卡, 要是人品好的话, 系统已经自带了你的网卡的驱动程序. 不然就要先搞定无线网卡的驱动再说. 不然后面的步骤也就没必要了. 2. 看一下你的无线网卡叫什么: iw ...
- 基于时间点恢复数据库stopat
create database newtestdb use newtestdbgo drop table t1go create table t1 (id int not null identity( ...
- 安装交叉编译器arm-linux-gcc
需要交叉编译环境故安装交叉编译环境 1.在宿主机的/usr/local/arm目录存放交叉编译器 mkdir /usr/local/arm 2.解压交叉编译器包至/usr/l ...
- 【PyQuery】PyQuery总结
pyquery库是jQuery的Python实现,可以用于解析HTML网页内容, 官方文档地址是:http://packages.python.org/pyquery/. 二.使用方法 ? 1 fro ...
- 【crunch bang】增加壁纸图片文件
将你的壁纸图片复制到 ~/images/wallpapers/shared即可.当然你得在终端用cp命令,因为这个目录是有权限到
- Relative 定位与Absolute 定位实例
一直没有弄懂相对定位与绝对定位之间的关系,今天特来学习一下.本实践都是在360浏览器下测试所得. <!DOCTYPE html> <html> <head> < ...
- ubuntu安装遇到的问题
检查磁盘发现严重错误 解决办法 进入ubuntu启动菜单,选中*ubuntu后按e进入启动项编辑模式,找到ro rootflags=sync把ro改成rw,再按F10启动 启动后打开终端termina ...
- USB HID介绍【转】
本文转载自:http://blog.csdn.net/leo_wonty/article/details/6721214 HID是一种USB通信协议,无需安装驱动就能进行交互,在学习HID之前,先来复 ...
- Notepad++编辑Pyhton文件的自动缩进的问题(图文)
转自:http://www.xuebuyuan.com/1102224.html 这个问题一直困扰我很久,Python对缩进很敏感,一般建议缩进用空格,而 Notepad++的自动缩进是用的TAB,g ...
- JavaScript调试技巧之console.log()详解
JavaScript调试技巧之console.log()详解 对于JavaScript程序的调试,相比于alert(),使用console.log()是一种更好的方式,原因在于:alert()函数会阻 ...