1、ServletActionContext
ServletActionContext, 这个类继承自ActionContext, 所以它具有ActionContext的很多功能,不过更重要的是它提供了直接访问Servlet相关对象的功能,功能更强大,缺点是耦合程度提高了。
HttpServletRequest、ServletContext、PageContext、HttpServletResponse. HttpSession需要从HttpServletRequest实例中获取,并没有提供直接获取的方法。
2、测试代码
package com.test.action; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.servlet.jsp.PageContext; import org.apache.struts2.ServletActionContext; public class TestServletActionContextAction { public String execute() { // 1、HttpServletRequest HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); // 2、ServletContext ServletContext servletContext = ServletActionContext .getServletContext(); // 3、PageContext PageContext pageContext = ServletActionContext.getPageContext(); // 4、HttpServletResponse HttpServletResponse response = ServletActionContext.getResponse(); System.out.println("execute..."); return "success"; } }