struts2 获取request,response的三种方法

(1) 根据ActionContext

public class GetRequestAndSession {

public void getReq(){
Map request = (Map)ActionContext.getContext().get("request");
request.put("msg", "abcdefg");  //此处相当于request.setAttribute("msg","abcdefg");
}

public void getSession(){
Map response=(Map)ActionContext.getContext().get("session");
//或者ActionContext.getContext().getSession();
response.put("
str, "abcdefg");  //此处相当于response.setAttribute("str","abcdefg");
}

}

(2)实现ServletRequestAware、SessionAware接口

public class InterfaceImp implements ServletRequestAware,SessionAware{

private Map<String, Object> session;
private HttpServletRequest request;

@Override
public void setSession(Map<String, Object> session) {
this.session=session;
}


@Override
public void setServletRequest(HttpServletRequest requset) {
this.request=requset;
}

public void test(){
request.setAttribute("msg", "今天天气不错");
session.put("msg2", "今天天气一般");
}

}

(3)

public class GetByServlet {


public void getReqAndSession(){
HttpServletRequest request=ServletActionContext.getRequest();
HttpSession session= request.getSession();
}
}

你可能感兴趣的:(struts2.0)