【struts2】Action类中获取session,request,response对象

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

publicclass IoCServlet extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {

    private Map session;
    private HttpServletRequest request;
    private HttpServletResponse response;    

    //set注入Session,struts2把session封装成了一个Map
    publicvoid setSession(Map session) {
        this.session= session;
    }
    //set注入request对象
    publicvoid setServletRequest(HttpServletRequest request) {
        this.request = request;
    }
    //set注入response对象
    publicvoid setServletResponse(HttpServletResponse response) {
        this.response = response;
    }

    @Override
    public String execute() {        
        //方法中可以对session、request、response进行操作    

        ......

        return SUCCESS;
    }
}
posted @ 2017-05-14 11:50  SEC.VIP_网络安全服务  阅读(66)  评论(0编辑  收藏  举报