访问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. Android自定义ScrollView实现一键置顶功能

    效果图如下: (ps:动态图有太大了,上传不了,就给大家口述一下要实现的功能吧) 要实现的功能:当ScrollView向上滑动超过一定距离后,就渐变的出现一个置顶的按钮,当滑动距离小于我们指定的距离时 ...

  2. paper 62:高斯混合模型(GMM)参数优化及实现

    高斯混合模型(GMM)参数优化及实现 (< xmlnamespace prefix ="st1" ns ="urn:schemas-microsoft-com:of ...

  3. ANT命令总结(转载)

    1 Ant是什么? Apache Ant 是一个基于 Java的生成工具.生成工具在软件开发中用来将源代码和其他输入文件转换为可执行文件的形式(也有可能转换为可安装的产品映像形式).随着应用程序的生成 ...

  4. 绑定repeater时三目运算加特殊结果处理

    <%#((Convert.ToDouble().ToString() != ).ToString(%

  5. win7 chm 打开失败记录

    近期学习rails,制作的html帮助文件想生成chm文件,用了几个网上的html制作chm软件,生成的chm无法打开. 网上大部分解决方法是修改文件属性,Unlock之后可解决. 以前遇到过打不开, ...

  6. USB 设备类协议入门【转】

    本文转载自:http://www.cnblogs.com/xidongs/archive/2011/09/26/2191616.html 一.应用场合 USB HID类是比较大的一个类,HID类设备属 ...

  7. TI CC2541增加一个可读写, 又可以Notify的特征字

    参考这个博客: http://blog.csdn.net/feilusia/article/details/48235691 值得注意是, 测试前, 在手机中先取消对原有的设备的配对.

  8. webpack笔记_(1)_webpack 安装

    webpack不仅可以解析jsx,也可以将es6转换为es5语法.最终,它把这些代码都打包成一个叫bundle.js的文件,我们在html文件中只引入这么一个js文件就可以了! 打包后,引用的语法im ...

  9. Spring注解@Scheduled定时任务

    一.首先配置applicationContext-task.xml (1)添加 xmlns:task="http://www.springframework.org/schema/task& ...

  10. Linux按键驱动程序设计详解---从简单到不简单【转】

    转自:http://blog.csdn.net/coding__madman/article/details/51399353 版权声明:本文为博主原创文章,未经博主允许不得转载. 混杂设备驱动模型: ...