Action访问Servlet API

时间:2022-09-13 03:24:28

访问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;
    }
}