访问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的更多相关文章

  1. struts2 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用

    Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...

  2. struts2的action访问servlet API的三种方法

    学IT技术,就是要学习... 今天无聊看看struts2,发现struts2的action访问servlet API的三种方法: 1.Struts2提供的ActionContext类 Object g ...

  3. Struts 2读书笔记-----Action访问Servlet API

    Action访问Servlet API Struts2中的Action并没有和任何Servlet API耦合,这样框架更具灵活性,更易测试. 对于Web应用的控制器而言,不访问ServletAPI是几 ...

  4. Action访问Servlet API的三种方法

    一.为什么要访问Servlet API ? Struts2的Action并未与Servlet API进行耦合,这是Struts2 的一个改良,从而方便了单独对Action进行测试.但是对于Web控制器 ...

  5. 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用

    Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...

  6. Struts2笔记--Action访问Servlet API

    Web应用中通常需要访问的Servlet API就是HttpServletRequest.HttpSession和ServletContext,这三个接口分别代表JSP内置对象中的request.se ...

  7. Struts2学习笔记(五)——Action访问Servlet API

    在Strut2中访问Servlet API有三种方式: 1.通过ActionContext访问Servlet API,推荐使用这种,但是这种方案它获取的不是真正的事Servlet API. 步骤: 1 ...

  8. 关于Struts2自动装配和访问Servlet API

    自动装配 1.根据属性的getter和setter获取值  index.jsp <s:form action="hello" method="POST"& ...

  9. Action访问Servlet的API

    Action访问Servlet的API_,主要访问如下: 1.>获取request对象 2.>获取请求参数 3.>获取response对象,可用于传递cookie 3.>获取作 ...

随机推荐

  1. snmp getTable demo :iftable ipAddresstable

    package org.huangxf.snmp.test; import java.io.IOException; import java.util.List; import org.snmp4j. ...

  2. linux时间的查看与修改

    1.查看时间和日期 date 2.设置时间和日期 将系统日期设定成1996年6月10日的命令 date -s 06/22/96 将系统时间设定成下午1点52分0秒的命令 date -s 13:52:0 ...

  3. 夺命雷公狗---微信开发17----自定义菜单的事件推送,响应菜单的CLICK

    废话不多说,index.php 代码如下所示: <?php /** * wechat php test */ //define your token require_once "com ...

  4. SQL Server 最小化日志操作解析,应用

    Sql Server 中数据库在BULK_LOGGED/SIMPLE模式下的一些操作会采用最小化日志的记录方式,以减小tran log落盘日志量从而提高整体性能. 这里我简单介绍下哪些操作在什么样的情 ...

  5. java中枚举类的使用详解

    /* * 通过JDK5提供的枚举来做枚举类 */ public enum Direction2 { FRONT("前"), BEHIND("后"), LEFT( ...

  6. Akka.NET

    https://github.com/akkadotnet Akka是什么? 可扩展的分布式实时事务处理 编写正确的并发,容错和可扩展的应用程序是太难了.大多数时候,这是因为我们使用了错误的工具和错误 ...

  7. [转]Jexus的常用操作和基本配置

    转自http://www.cnblogs.com/xiaodiejinghong/archive/2013/04/05/3000404.html 3.Jexus的操作 经过两个章节关于Jexus的介绍 ...

  8. 微信开放平台API开发资料

    微信大概两年前开启了微信公众平台的API供开发者使用,从账号登陆.消息发送.用户账号管理.公众号菜单.客服接口.微信商店接口.用户卡券接口 以及微信支付接口.可以说是全方面覆盖了电商所需要的要素,与阿 ...

  9. ecshop发票不能使用出现flow.php on line 723等报错

    最模板给客户ecshop网站做编码转换出现个问题,网站在点结算页面出现Warning: Invalid argument supplied for foreach flow.php on line 7 ...

  10. samba服务器源码安装(非rpm)

    首先我们创建一个文档,边安装配置samba,边写教程. 从www.samba.org下载samba最新源码包,我下载的是samba-3.0.7.tar.gz,把它放在我的目录的中/root/lova/ ...