方法1:
implements ServletRequestAware, ServletResponseAware
public class Hello implements ServletRequestAware, ServletResponseAware { private HttpServletRequest request; private HttpServletResponse response; @Override public void setServletRequest(HttpServletRequest httpServletRequest) { this.request = httpServletRequest; } @Override public void setServletResponse(HttpServletResponse httpServletResponse) { this.response = httpServletResponse; } }
方法2
在普通java类中通过ServletActioContext对象来获取