struts2 Action中获取request, response对象的 几种 方法

时间:2023-02-04 22:02:22
 

struts2 Action中获取request, response对象的方法 
第一种方法:

 

 

ActionContext ctx = ActionContext.getContext();          

 

  HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);          

 

  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);          

 

//ServletActionContext.APPLICATION;        

//ServletActionContext.SESSION;        

//ServletActionContext.PAGE_CONTEXT;       

 


第二种方法:
实现     ServletRequestAware

public class TestAction implements ServletRequestAware{  

/**---*/

public void setServletRequest(HttpServletRequest request) {  

// 要复写这个方法

        request.setAttribute("key","value");   

    }  

public class TestAction implements ServletRequestAware{

      /**---*/

      public void setServletRequest(HttpServletRequest request) {
// 要复写这个方法

request.setAttribute("key","value");
}

}


第三种:更简单的一种。。

 

HttpServletRequest request = ServletActionContext.getRequest();  //相应的可以得到response