专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

Struts2获得Servlet的API(获得Servlet对象的三种方式)

1、ActionContext对象方式

(1)ActionContext对象的作用:

ActionContext对象是Struts2的一个内置对象,通过该对象可以获得Servlet的对象,如:request、response等,ActioContext对象本质上是一个Map集合。

(2)ActionContext中常见的对象

96_1.png

其中,attr域是request域、response域和application域,三个域的累计。

(3)生命周期:

每次请求都会创建一个ActionContext对象,请求处理结束该ActionContext对象销毁,其他的如:request、response的生命周期和以前一样。

(4)ActionContext对象的获取

ActionContext对象创建之后会与当前线程进行绑定,一个线程有一个唯一的ActionContext,因此,我们可以从ThreadLocal中获取ActionContext对象。

2、ActionContext方式:域对象值的设定与取出

(1)对每个域设定值:

public class ApiAction extends ActionSupport {
       public String execute() {
           Map<String,Object> requestScope= (Map<String, Object>) ActionContext.getContext().get("request");
           requestScope.put("name","我是用常规的方式来获得request域");

           ActionContext.getContext().put("name","我是用ActionContext的方式来获得request域");

           Map<String,Object> sessionScope=ActionContext.getContext().getSession();
           sessionScope.put("name","我是通过ActionContext的方式来获得的session域");

           Map<String,Object> applicationScope=ActionContext.getContext().getApplication();
           applicationScope.put("name","我是通过ActionContext的方式来获得的application域");
           return "success";
       }
}

其中request域的获取有两种方法,但是第一种方法较为复杂较少使用,第二种方法为获得ActionContext域,因为两个域的生命周期相同,效果也是一样的。

(2)从域中取值:

<body>
        ${requestScope.name}<br>
        ${request.name}<br>
        ${sessionScope.name}<br>
        ${applicationScope.name}<br>
</body>

96_2.png

3、ServletActionContext对象

(1)获取原生的servlet对象

      public String execute() {
           HttpServletRequest request=ServletActionContext.getRequest();

           HttpSession session=request.getSession();

           HttpServletResponse response=ServletActionContext.getResponse();

           ServletContext servletContext=ServletActionContext.getServletContext();

           return "success";
       }

(2)生命周期

ServletActionContext类直接继承了ActionContext类,两者的生命周期相同。

public class ServletActionContext extends ActionContext implements StrutsStatics {

4、继承接口的方式

public class ApiAction extends ActionSupport implements ServletRequestAware, ServletResponseAware {
       private HttpServletRequest httpServletRequest;
       private HttpServletResponse httpServletResponse;
       public String execute() {
           return "success";
       }
        @Override
        public void setServletRequest(HttpServletRequest httpServletRequest) {
               this.httpServletRequest=httpServletRequest;
        }

        @Override
        public void setServletResponse(HttpServletResponse httpServletResponse) {
               this.httpServletResponse=httpServletResponse;
        }
}

即获取一个对象就要实现一个相应的接口。

5、总结

较为常用的是第一种方式,后两种方式都是在第一种方式之上建立的。

文章永久链接:https://tech.souyunku.com/23125

未经允许不得转载:搜云库技术团队 » Struts2获得Servlet的API(获得Servlet对象的三种方式)

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们