struts2中获取request、session、response

HttpServletRequest request = ServletActionContext.getRequest();

HttpServletResponse response = ServletActionContext.getResponse();

HttpSession session = request.getSession();


注意:使用request报空指针,原因是把request做成员变量了, ServletActionContext.getResponse()未执行,导致request未被初始化。
代码如下:

public class TaskAction implements Action{
	
	HttpServletRequest request = ServletActionContext.getRequest();
	HttpServletResponse response = ServletActionContext.getResponse();
	public void setTask(DxCollectTask task) {
		this.task = task;
	}

	public String execute() throws Exception {
                   //报空指针
		String name = request.getParameter("name");
		return null;
         }
}

你可能感兴趣的:(java)