在做struts2相关项目时,获得HttpServletRequest / HttpSession / ServletContext / HttpServletResponse对象

来源网站:http://chen106106.iteye.com/blog/1318596


方法一,通过ServletActionContext.类直接获取: 
public String rsa() throws Exception{ 
HttpServletRequest request = ServletActionContext.getRequest(); 
ServletContext servletContext = ServletActionContext.getServletContext(); 
request.getSession() 
HttpServletResponse response = ServletActionContext.getResponse(); 
return "scope"; 

方法二,实现指定接口,由struts框架运行时注入: 
public class HelloWorldAction implements ServletRequestAware, ServletResponseAware, ServletContextAware{ 
private HttpServletRequest request; 
private ServletContext servletContext; 
private HttpServletResponse response; 
public void setServletRequest(HttpServletRequest req) { 
this.request=req; 

public void setServletResponse(HttpServletResponse res) { 
this.response=res; 

public void setServletContext(ServletContext ser) { 
this.servletContext=ser; 

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted on 2015-05-26 13:41  六月心悸  阅读(156)  评论(0编辑  收藏  举报