ServletContext和Struts2中的ActionContext/ServletActionContext区别

解锁PyCharm+Deepseek:本地部署AI编程超能力 在本次探索中,我们成功地在 PyCharm 中实现了 DeepSeek 的本地化部署,这一过程虽然涉及多个步骤,但每一步都为我们开启 AI 编程大门奠定了基础。从安装最新版 PyCharm,确保我们拥有一个功能强大且兼容性良好的开发环境,到注册 DeepSeek 账号并获取 API Key,这是连接我们与强大 AI 能力的关键纽带。接着,在 PyCharm 中安装 Continue 插件并进行精细配置,让 DeepSeek 能够深度融入我们的编程流程。 阅读详情

ServletContext

  • ServletContext从他的package信息可以看出,它是标准的JavaEE WebApplication类库
javax.servlet.ServletContext
  • ServletContext提供了标准的Servlet运行环境,其实就是一些servlet和web container进行通信的方法
public interface ServletContext {

 // Returns the URL prefix for the ServletContext.
 public String getServletContextName();

 //Returns the ServletContext for the uri.
 public ServletContext getContext(String uri);
 
 //Returns the context-path for the web-application.
 public String getContextPath();
 
 //Returns the real file path for the given uri.
 public String getRealPath(String uri);
 
 public RequestDispatcher getRequestDispatcher(String uri);
 public RequestDispatcher getNamedDispatcher(String servletName);
public Object getAttribute(String name);
public Enumeration getAttributeNames();
public void setAttribute(String name, Object value);
public void removeAttribute(String name);
  • 注意:一个ServletContext对应一个命名空间的servlet( 比如/struts下的所有servlet),是被所有servlet共享的.

There is one context per “web application” per Java Virtual Machine.
(A “web application” is a collection of servlets and content installed under a specific subset of the server’s URL namespace such as /catalog and possibly installed via a .war file.)

  • ServletContext被包含在ServletConfig对象中,ServletConfig对象通常被servlet或filter的init()方法读取

ServletConfig.getServletContext()
filterconfig.getServletContext()


ActionContext来源于Struts2 与 Struts1的本质不同.
struts1时,由一个servlet (servlet org.apache.struts.action.ActionServlet)处理所有的*.do
struts2时,由一个filter(org.apache.struts2.dispatcher.FilterDispatcher)处理所有的请求
struts1 仍旧属于servlet范畴,struts1 action 其本质仍是servlet.
struts2 action 已经是普通的Java bean了,已经跳出了servlet 框架
ActionContext就是为了弥补strtus2 action跳出标准servlet框架而造成的和WEB环境失去联系的缺陷

ActionContext的主要作用:

  • 提供Web环境Context
  • 解决线程安全问题
  • 解决一些和其他框架或容器(siteMesh,webLogic)的兼容问题


分析ActionContext源码

 public class ActionContext implements Serializable {
  //////////ThreadLocal模式下的ActionContext实例,实现多线程下的线程安全///////////////

     static ThreadLocal actionContext = new ThreadLocal();
     
     //Sets the action context for the current thread.
     public static void setContext(ActionContext context) {
         actionContext.set(context);
     }
     //Returns the ActionContext specific to the current thread.
     public static ActionContext getContext() {
         return (ActionContext) actionContext.get();
     }
 ///////////////定义放置”名/值对”的Map容器,这是ActionContext的主要功能/////////////// 
  
     Map<String, Object> context;
              
     // constractor    
     // Creates a new ActionContext initialized with another context.
     public ActionContext(Map<String, Object> context) {
         this.context = context;
     }
     
     public void setContextMap(Map<String, Object> contextMap) {
         getContext().context = contextMap;
     }
     public Map<String, Object> getContextMap() {
         return context;
     }

//Returns a value that is stored in the current ActionContext by doing a lookup using the value’s key.
     public Object get(String key) {
         return context.get(key);
     }
 //Stores a value in the current ActionContext. The value can be looked up using the key.
     public void put(String key, Object value) {
         context.put(key, value);
     }
///////////////////将各种功能属性放置入Map容器中/////////////////////
            
     //action name, Constant for the name of the action being executed.
     public static final String ACTION_NAME = “com.opensymphony.xwork2.ActionContext.name”;
 
     // ognl value stack
     public static final String VALUE_STACK = ValueStack.VALUE_STACK;
 
     public static final String SESSION = “com.opensymphony.xwork2.ActionContext.session”;
     public static final String APPLICATION = “com.opensymphony.xwork2.ActionContext.application”;
     public static final String PARAMETERS = “com.opensymphony.xwork2.ActionContext.parameters”;
     public static final String LOCALE = “com.opensymphony.xwork2.ActionContext.locale”;
     public static final String TYPE_CONVERTER = “com.opensymphony.xwork2.ActionContext.typeConverter”;
     public static final String ACTION_INVOCATION = “com.opensymphony.xwork2.ActionContext.actionInvocation”;
     public static final String CONVERSION_ERRORS = “com.opensymphony.xwork2.ActionContext.conversionErrors”;
     public static final String CONTAINER = “com.opensymphony.xwork2.ActionContext.container”;
 
 ////// 各种Action主属性:ActionName, ActionInvocation(调用action的相关信息), ognl value stack///
             
     //Gets the name of the current Action.
     public String getName() {
         return (String) get(ACTION_NAME);
     }
     //Sets the name of the current Action in the ActionContext.
     public void setName(String name) {
         put(ACTION_NAME, name);
     }
     
     //Sets the action invocation (the execution state).
     public void setActionInvocation(ActionInvocation actionInvocation) {
         put(ACTION_INVOCATION, actionInvocation);
     }
     public ActionInvocation getActionInvocation() {
         return (ActionInvocation) get(ACTION_INVOCATION);
     }
 
     // Sets the OGNL value stack.
     public void setValueStack(ValueStack stack) {
         put(VALUE_STACK, stack);
     }
     //Gets the OGNL value stack.
     public ValueStack getValueStack() {
         return (ValueStack) get(VALUE_STACK);
     }

////////////////各种 request请求包含的内容////////////////////
     //Returns a Map of the HttpServletRequest parameters
     public Map<String, Object> getParameters() {
         return (Map<String, Object>) get(PARAMETERS);
     }
     public void setParameters(Map<String, Object> parameters) {
         put(PARAMETERS, parameters);
     }    
    
     public void setSession(Map<String, Object> session) {
         put(SESSION, session);
     }
     public Map<String, Object> getSession() {
                  return (Map<String, Object>) get(SESSION);
     }      
     public void setApplication(Map<String, Object> application) {
         put(APPLICATION, application);
     }
     public Map<String, Object> getApplication() {
         return (Map<String, Object>) get(APPLICATION);
     }
 
     public void setConversionErrors(Map<String, Object> conversionErrors) {
         put(CONVERSION_ERRORS, conversionErrors);
     }
     public Map<String, Object> getConversionErrors() {
         Map<String, Object> errors = (Map) get(CONVERSION_ERRORS);
 
         if (errors == null) {
             errors = new HashMap<String, Object>();
             setConversionErrors(errors);
         }
         return errors;
     }
 
     //Sets the Locale for the current action.
     public void setLocale(Locale locale) {
         put(LOCALE, locale);
     }
     public Locale getLocale() {
         Locale locale = (Locale) get(LOCALE);
 
         if (locale == null) {
             locale = Locale.getDefault();
             setLocale(locale);
         }
 
         return locale;
     }
     public void setContainer(Container cont) {
         put(CONTAINER, cont);
     }
     public Container getContainer() {
         return (Container) get(CONTAINER);
     }
    
     public <T> T getInstance(Class<T> type) {
         Container cont = getContainer();
         if (cont != null) {
             return cont.getInstance(type);
         } else {
             throw new XWorkException(“Cannot find an initialized container for this request.”);
         }
     }
 }



ServletActionContext 其实是ActionContext的子类,其功能脱胎于ActionContext,对ActionContext的方法做了一定的包装,提供了更简便直观的方法

 public class ServletActionContext extends ActionContext implements StrutsStatics {
/////////////////Servlet Context 提供了多种操作ActionContext的静态方法,使获得Web对象更方便

        //HTTP servlet request
      public static void setRequest(HttpServletRequest request) {
          ActionContext.getContext().put(HTTP_REQUEST, request);
      } 
      public static HttpServletRequest getRequest() {
          return (HttpServletRequest) ActionContext.getContext().get(HTTP_REQUEST);
      }
 
      //HTTP servlet response
      public static void setResponse(HttpServletResponse response) {
          ActionContext.getContext().put(HTTP_RESPONSE, response);
      } 
      public static HttpServletResponse getResponse() {
          return (HttpServletResponse) ActionContext.getContext().get(HTTP_RESPONSE);
      }
 
      //servlet context.
      public static ServletContext getServletContext() {
          return (ServletContext) ActionContext.getContext().get(SERVLET_CONTEXT);
      } 
      public static void setServletContext(ServletContext servletContext) {
          ActionContext.getContext().put(SERVLET_CONTEXT, servletContext);
      }


Probability and Statistics - 4th edition 立即下载

相关推荐

Cisco思科 电子科技大学计算机网络 SRWE 期末技能考试 (PTSA)

电子科技大学2024春_SRWE_计算机网络系统 SRWE 期末技能考试 (PTSA)

2301_76745065的博客 5113

ServletActionContext

有些时候,根据功能需要,在Action中必须能获取到Servlet相关的API。 比如要操作Cookie,这个时候就要用ServletActionContext了。 1.ServletActionContext概述 我们可以先看下它的源码: package org.apache.struts2; import com.opensymphony.xwork2.ActionContext; impor...

c1776167012的博客 1533

实战解析:如何利用MDFEND与Weibo21数据集构建多领域假新闻检测系统

本文实战解析了如何利用MDFEND模型与Weibo21数据集构建多领域假新闻检测系统。文章深入剖析了MDFEND的领域门控与专家混合机制,并提供了从数据预处理、模型理解到训练部署的完整指南,旨在解决通用模型在不同领域新闻检测中的性能衰减问题,提升检测的准确性与适应性。

vulkan6gpu的博客 590

ActionContext和ServletActionContext解析

1.ActionContext xwork框架的ActionContext是Action执行时的上下文,存放Action执行时需要用到的对象。在使用webwork时,其中放有Parameter、Session、ServletContext、Locale等信息。这样,webwork负责将Servlet相关数据转换为与ServletAPI无关的Map对象(即ActionContext),使得xwork的Action实现与web层、逻辑层与表现层的解耦。 2.ServletActionContext 提供直接与S

Aaron_Yang 1099

【ZZULIOJ】郑州轻工业大学oj题解整合

写在前面 关于ZZULIOJ题解 学习编程,想要巩固无非就是刷题或者做项目,然后因为我大一开始是刷郑轻的题目,就想着自己整合一下自己写的题目。既是复习也是笔记吧。 题解(内有题目描述) 1000 整数a+b 1001 植树问题 1002 简单多项式求值 1003 两个整数的四则运算 1004 三位数的数位分离 1005 整数幂 1006 求等差数列的和 1007 鸡兔同笼 1008 美元和人民币 1009 求平均分 1010 求圆的周长和面积 1011 圆柱体表面积 1012 求绝对值 1013 求两点间

蒟蒻乱写的博客 29万+

STM32H743内存告急?手把手教你用CubeMX+FMC外扩32MB SDRAM(W9825G6KH-6)

本文详细介绍了如何在STM32H743上使用CubeMX和FMC外扩32MB SDRAM(W9825G6KH-6),解决内存不足问题。从硬件选型、CubeMX配置到初始化代码实现,提供完整的配置教程和性能优化技巧,帮助开发者高效扩展内存,适用于图形界面和复杂算法处理。

weixin_30568591的博客 508

ServletContext、ActionContext、ServletActionContext介绍

一 、ServletContext 两个问题: 1.大家在访问某个网站的时候,往往都会看到网站的首页面显示您是第几位浏览者(网站计数器),这是怎么实现的? 2.我们在访问某个bbs网站的时候,往往会显示有多少人在线,这是怎么实现的? 可能我们会想到的常规实现思路:数据库或者文件。这种做法比较简单,但是却会对数据库或者文件访问过于频繁,开销比较大。 解决之道是用ServletContext 1.什么...

person_on_pyramid的博客 3204

插件系统架构解析:VS Code与Obsidian设计对比

插件系统是现代软件开发中实现功能扩展的核心机制,其本质是通过标准化接口实现宿主程序与扩展模块的解耦。从技术原理看,典型的插件架构包含宿主环境、接口契约和功能实现三个关键组件,这种设计模式在VS Code、Obsidian等主流编辑器中得到广泛应用。在工程实践中,不同系统对接口标准化程度的要求差异显著:VS Code采用严格的package.json声明规范,而Obsidian则支持更灵活的manifest配置方式。良好的插件架构能显著提升软件的可维护性和生态活跃度,特别是在需要频繁功能迭代的开发工具领域。本

weixin_31822733的博客 274

Struts2中ServletActionContext.getServletContext().getRealPath("/")空指针

准备工作:xwork源码一份(我的版本是2.0.4)。 1.当时我是这么建立这个类的 Java代码 publicclassStaticActionextendsActionSupport{ static{ logDir=ServletActionContext.getServletContext().getR...

weixin_30485291的博客 659

从零开始之驱动发开、linux驱动(七、字符驱动之led最终版)

我们上一接用的gpiolib非常杂乱,使用了两种 1.各硬件厂商实现的,如s3c_gpio_cfgpin(S5PV210_GPJ0(5), S3C_GPIO_SFN(1)) 2.系统公共的,如gpio_set_value(S5PV210_GPJ0(4),val) 其实公共的也是对各硬件厂商的封装,用那个都可以,个人建议用系统公共的,应为这样换芯片厂商,不影响任何使用。   2.错误处理基...

To_run_away的博客 2393

STM32 USB键盘实现指南

在STM32上实现键盘功能可以通过USB HID(人机接口设备)协议来实现,使STM32设备能被计算机识别为标准键盘。通过上述步骤,您可以在STM32上实现一个功能完整的USB键盘。关键点包括:正确配置USB HID设备实现键盘矩阵扫描处理按键事件并生成HID报告优化按键响应和消抖处理添加额外功能如宏键和LED反馈这个实现可以作为基础,进一步扩展为游戏键盘、宏键盘或特殊输入设备。

suny8的专栏 979

Struts2

1.Struts2入门 1.1 入门案例 添加struts2相关依赖 添加web.xml,加载struts2配置 编写index.jsp页面 编写Action控制器 编写struts.xml文件 1.1.1 添加struts2相关依赖 在pom.xml文件中加入struts2的依赖代码 <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</a

qq_52269841的博客 531

【Android性能优化】Android App 冻结与内存压缩:CachedAppOptimizer 源码级解析

本文基于 AOSP Android 16 源码,以 `CachedAppOptimizer` 为核心,深度解析 Android 对"后台缓存应用"的两大治理手段——**进程冻结(Freezer)**与**内存压缩(Compaction)**。文中完整还原冻结的 debounce 去抖动机制、`mEarliestFreezableTime` 的最早可冻结时刻算法、粘性冻结(Sticky Freeze)的语义,以及解冻时必须"先解 binder 再解进程"的顺序奥义。

weixin_44081096的博客 203

ActionContext和ServletActionContext的区别

7.4  ActionContext和ServletActionContext 7.4.1  再探ActionContext 前面已经了解到ActionContext是Action执行时的上下文,里面存放着Action在执行时需要用到的对象,我们也称之为广义值栈。        Struts2在每次执行Action之前都会创建新的ActionContext,在同一个线程里Acti

sunny 4104

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

ServletContext,ActionContext与ServletActionContext详解

ServletContext ServletContext从他的package信息可以看出,它是标准的JavaEE WebApplication类库 javax.servlet.ServletContext ServletContext提供了标准的Servlet运行环境,其实就是一些servlet和web container进行通信的方法

Golden_lion的博客 3757

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Struts2中ActionContext、 ServletActionContext详解

1. ActionContext ActionContext是被存放在当前线程中的,获取ActionContext也是从ThreadLocal中获取的。所以在执行拦截器、 action和result的过程中,由于他们都是在一个线程中按照顺序执行的,所以可以可以在任意时候在ThreadLocal中获取 ActionContext。 ActionContext包括了很多信息,比如S

lilun517735159的博客 518

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

ServletContext,ActionContext,ServletActionContext

ServletContext ServletContext从他的package信息可以看出,它是标准的JavaEE WebApplication类库 javax.servlet.ServletContext ServletContext提供了标准的Servlet运行环境,其实就是一些servlet和web container进行通信的方法 p

HeartMonth的专栏 686

ActionContext,ServletContext和ServletActionContext有什么区别?

票赞成 他们之间有很多区别。 ServletContext 从ServletContext的包( javax.servlet.ServletContext )我们可以知道它是标准的JavaEE WebApplication类库。 ServletContext提供了标准的Servlet运行时环境。 实际上是servlet与web容器进行通信的一些方法。 p

paullinjie的博客 3086

ServletActionContext.getRequest().getSession()和ActionContext.getContext().getSession,两个session的区别

直接原因: ActionContext.getContext().getSession(); 这个方法获取的session是struts封装过的一个Map类型的session,只能调用put()方法缓存数据。 ServletActionContext.getRequest().getSession(); 这个方法获取的session是原生的session,可以调用setAttribute(...

qin2460855098的博客 1万+

(转)ActionContext和ServletActionContext

前面已经了解到ActionContext是Action执行时的上下文,里面存放着Action在执行时需要用到的对象,我们也称之为广义值栈。 Struts2在每次执行Action之前都会创建新的ActionContext,在同一个线程里ActionContext里面的属性是唯一的,这样Action就可以在多线程中使用。 1:ActionContext的线程安全性 ...

125

ServletContext,ActionContext,ServletActionContext的区别

最近在学习Struts2的时候,在获取servlet对象的时候同时出现ServletContext,ActionContext,ServletActionContext,一时倍感纠结,也是做个总结ServletContext:从字面上理解ServletContext指的是servlet对象,他属于servlet的范围,代表这个servlet对象ActionContext:顾名思义ActionCont

JunXia 400

struts2中 ServletActionContext、ActionContext和ServletContext三者的区别

1. ActionContext        在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息,甚至需要直接对JavaServlet Http的请求(HttpServletRequest),响应(HttpServletResponse)操作. 我们需要在Action中取得re

qq_28053779的博客 388

struts2 中的ActionContext与ServletActionContext

在Struts2中,进行业务控制的类就是ogc

funcy的专栏 1332

ServletContext,ActionContext,ServletActionContext 的区别

http://blog.csdn.net/ocean1010/article/details/6160159 ServletContext从他的package信息可以看出,它是标准的JavaEE WebApplication类库 ServletContext提供了标准的Servlet运行环境,其实就是一些servlet和web container进行通信的方法 ActionConte

weixin_35772453的博客 221

struts2中 ServletActionContext与ActionContext区别

1. ActionContext 在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息,甚至需要直接对JavaServlet Http的请求(HttpServletRequest),响应(HttpServletResponse)操作. 我们需要在Action中取得request请求参数”u

broccoli2的博客 440

ActionContext和ServletActionContext区别

ActionContext和ServletActionContext区别 1.ActionContext:是struts2的上下文容器,与线程一一对应。 (1)是存放Map型数据 ActionContext ctx = ActionContext.getContext(); Map request = ctx.get(“request”); request.put(“name”,va

qq_36015612的博客 283
上一篇: Spring 注解注入概述
下一篇: Action动作类以及struts.xml配置解析
Hanlonia
博客等级 码龄10年 0粉丝 3原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值