ServletContext,ActionContext,ServletActionContext

javaWEB总结(4): ServletContext对象 介绍 ServletContext对象的一些重要方法的示例代码 立即下载

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);
      }
用Python玩转农业大数据:从Landsat遥感影像到作物产量预测实战教程 本文详细介绍了如何利用Python处理Landsat遥感影像数据,并结合土壤信息构建作物产量预测模型。从环境搭建、数据预处理到模型训练与可视化,提供了一套完整的农业大数据分析实战方案,帮助读者掌握遥感影像处理和机器学习在精准农业中的应用。 阅读详情

相关推荐

安路科技FPSoC开发实战:从芯片特性到边缘计算应用

本文深入解析安路科技FPSoC芯片在边缘计算领域的开发实战,重点介绍SALDRAGON系列芯片的异构计算架构优势,包括RISC-V/ARM处理器与FPGA的协同工作。通过工业控制和智能视觉应用案例,展示硬件加速实现与低功耗设计技巧,帮助开发者高效利用FPSoC进行边缘计算应用开发。

weixin_29248611的博客 383

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

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

weixin_30485291的博客 659

别再手动配电脑了!手把手教你用Windows Server 2022搭建企业AD域(附DNS配置避坑指南)

本文详细介绍了如何使用Windows Server 2022搭建企业AD域,提升20+终端的管理效率。通过实战案例和避坑指南,解析AD域的核心价值、部署步骤及DNS配置技巧,帮助企业实现集中权限管理、安全策略统一部署,显著降低IT运维成本。

weixin_30652897的博客 357

java 获取context_java类中获取ServletContext的方法

起因是我想要获取一个相对路径,需要用到servletContext的getRealPath()方法,于是上网搜索,找到两种方法来获取ServletContext.第一种方法是这样的:ServletActionContext.getServletContext();后来发现这种方法只有在从浏览器打开的时候才能获取到ServletContext,否则在普通的java类中会报空指针错误(找不到Servl...

weixin_39942474的博客 458

中移物联ML307C模组OPENCPU开发环境搭建全攻略

本文详细介绍了中移物联ML307C模组OPENCPU开发环境搭建的全过程。从OPENCPU模式的核心优势解析,到SDK获取、Python与scons工具安装,再到解读SDK目录结构与实战编译命令,最后提供常见问题避坑指南,帮助开发者快速构建高效的物联网应用编译环境。

weixin_29168393的博客 761

常见机械臂模型不用找!Mujoco这儿都有!

高质量的模型集合,franka panda,so-arm100,ur之类的机械臂都有。常见机械臂模型不用找!Mujoco这儿都有!大概346M,网速不行的可以下载zip。

darcsdn 1008

ServletActionContext.getServletContext().getRealPath("upload")解析

ServletActionContext.getServletContext().getRealPath("upload") 1.获取ServletAction上下文对象,getServletContext()获取Servlet上下文对象,getRealPath("upload")获取upload的绝对路径。 2.这个使用于文件上传的时候,把上传的文件存放到项目下的upload文件夹里面,而上...

ITCBL 5301

【液晶专题】从外挂到In-Cell/ On-Cell/ OGS再也不会傻傻分不清楚了

触控技术经历了从外挂式到内嵌式的革新,主要方案包括外挂式(Out-Cell)、OGS、On-Cell和In-Cell。

一点硬件 1258

ServletContext的学习

public void _jspService(HttpServletRequest request, HttpServletResponse response)         throws java.io.IOException, ServletException {     PageContext pageContext = null;     HttpSession sessio

javaMov的专栏 613

C#工控上位机实战:30天落地汽车焊装线PLC数据采集系统,方案+架构+踩坑全实录

1247个点位全量采集,平均延迟150ms左右,满足所有实时性要求累计运行4000+小时,数据准确率99.95%,断网补传零丢失彻底替代人工抄表,原来每班需要2个人统计数据,现在自动生成报表,生产统计效率提升80%做工业软件开发久了会发现,代码能力只是基础,真正的竞争力是解决现场问题的能力。你要懂PLC、懂工艺、懂电气,甚至还要会接网线、查接地、调串口。30天的项目,一半时间在写代码,一半时间在现场解决各种意料之外的问题。很多刚入行的朋友问我,工业上位机开发的核心是什么?

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 1155

YOLO11改进-注意力-引入级联组注意力机制(Cascaded Group Attention, CGA)

输入特征拆分:输入特征进入 CGA 模块后,被均匀拆分为个部分,每个部分作为一个注意力头的输入,这样每个头关注输入特征的不同部分,实现特征的并行处理,同时为提高注意力多样性奠定基础。注意力头计算单元:每个注意力头包含三个线性投影层、、用于将输入特征映射到不同子空间,以及计算自注意力的单元。在计算过程中,通过注意力头之间的级联操作,前一个头的输出会累加到下一个头的输入上,实现特征的逐步细化。

qq_64693987的博客 2492

ServletContext、ActionContext、ServletActionContext介绍

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

person_on_pyramid的博客 3204

Android 12.0 系统systemui状态栏下拉左滑显示通知栏右滑显示控制中心模块的流程分析

在android12.0的系统rom定制化开发中,在系统原生systemui进行自定义下拉状态栏布局的定制的时候,需要在systemui下拉状态栏下滑的时候,根据下滑坐标来 判断当前是滑出通知栏还是滑出控制中心模块,所以就需要根据屏幕宽度,来区分x坐标值为多少是左滑出通知栏或者右滑出控制中心模块,接下来分析下 状态栏下滑坐标等相关功能代码分析

安卓兼职framework和app工程师的博客 2773

ServletContext,ActionContext与ServletActionContext详解

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

Golden_lion的博客 3757

计算机控制技术及应用

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

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

ActionContext 、ServletContext 和 ServletActionContext 的区别

上下文范围有 request, session, application,而 ServletContext 就是指的 application,可以在在Web应用范围内存取共享数据,是在Web应用程序装载时初始化的。 可以通过 ServletActionContext. getServletContext() 或者 ActionContext. getContext().getApplicatio

HappyRocking的专栏 1985

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

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

ServletContext,ActionContext,ServletActionContext的区别

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

待你归来的博客 405

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

ActionContext,ServletContext和ServletActionContext有什么区别?

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

paullinjie的博客 3086

ServletContext、ActionContext、ServletActionContext区别

Servlet 描述 这是一组接口,规范(sun公司并没有去实现它) 它的实现就是由各大服务器厂商(Tomcat)去实现 Tomcat是一个javaweb服务器,也是一个Servlet容器 Servlet是所有创建,运行,销毁都由Servlet容器完成 Servlet 默认是单例模式,在web 容器中只创建一个实例,所以多个线程同时访问servlet的时候,Servlet是线程不安全的 生命周期 ...

借物小人的博客 8048

转:ServletContext,ActionContext,ServletActionContext

转:ServletContext,ActionContext,ServletActionContext ServletContext ServletContext从他的package信息可以看出,它是标准的JavaEE WebApplication类库 javax.servlet.ServletContext ServletCon...

weixin_30827565的博客 108

ServletContext,ActionContext,ServletActionContext (关系及联系)

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

cnq2328的专栏 377

ServletContext,ActionContext和ServletActionContext比较

首先来源不同,ServletContext来源于标准J2EE,是Servlet的最大接口,最常见的实现是jsp的application。而ActionContext和ServletActionContext来源于Struts。 ActionContext就是为了弥补strtus2 action跳出标准servlet框架而造成的和WEB环境失去联系的缺陷(引用)。ServletActionCont

bingbushangshu的博客 280
上一篇: serializable
下一篇: EL表达式
心中月
博客等级 码龄14年 0粉丝 6原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值