转自:http://www.blogjava.net/fancydeepin/archive/2013/03/31/java-ee-context.html

在 java 中, 常见的 Context 有很多,

像: ServletContext, ActionContext, ServletActionContext, ApplicationContext, PageContext, SessionContext ...

那么, Context 究竟是什么东西呢? 直译是上下文、环境的意思。比如像: "今天我收到了一束花, 男朋友送的!" 又或者 "今天我收到了一束花, 送花的人送错了的!"

同样是收到一束花, 在不同的上下文环境中表达的意义是不一样的。

同样的, Context 其实也是一样, 它离不开所在的上下文环境, 否则就是断章取义了。

另外, 在网络上也有些人把 Context 看成是一些公用信息或者把它看做是一个容器的, 个人觉得这种解释稍好。

接下来说说 ServletContext, ActionContext, ServletActionContext。
 
 1> ServletContext

一个 WEB 运用程序只有一个 ServletContext 实例, 它是在容器(包括 JBoss, Tomcat 等)完全启动 WEB 项目之前被创建, 生命周期伴随整个 WEB 运用。

当在编写一个 Servlet 类的时候, 首先是要去继承一个抽象类 HttpServlet, 然后可以直接通过 getServletContext() 方法来获得 ServletContext 对象。

这是因为 HttpServlet 类中实现了 ServletConfig 接口, 而 ServletConfig 接口中维护了一个 ServletContext 的对象的引用。

利用 ServletContext 能够获得 WEB 运用的配置信息, 实现在多个 Servlet 之间共享数据等。

eg:

 <?xml version="1.0" encoding="UTF-8"?>

   <context-param>
<param-name>url</param-name>
<param-value>jdbc:oracle:thin:@localhost:1521:ORC</param-value>
</context-param>
<context-param>
<param-name>username</param-name>
<param-value>scott</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>tigger</param-value>
</context-param> <servlet>
<servlet-name>ConnectionServlet</servlet-name>
<servlet-class>net.yeah.fancydeepin.servlet.ConnectionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ConnectionServlet</servlet-name>
<url-pattern>/ConnectionServlet.action</url-pattern>
</servlet-mapping> <servlet>
<servlet-name>PrepareConnectionServlet</servlet-name>
<servlet-class>net.yeah.fancydeepin.servlet.PrepareConnectionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PrepareConnectionServlet</servlet-name>
<url-pattern>/PrepareConnectionServlet.action</url-pattern>
</servlet-mapping> </web-app> package net.yeah.fancydeepin.servlet; import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class PrepareConnectionServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void init() throws ServletException { ServletContext context = getServletContext();
String url = context.getInitParameter("url");
String username = context.getInitParameter("username");
String password = context.getInitParameter("password");
context.setAttribute("url", url);
context.setAttribute("username", username);
context.setAttribute("password", password);
} protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{ doPost(request, response);
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.sendRedirect("ConnectionServlet.action");
}
} package net.yeah.fancydeepin.servlet; import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet; public class ConnectionServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { ServletContext context = getServletContext();
System.out.println("***************************************");
System.out.println("URL: " + context.getAttribute("url"));
System.out.println("Username: " + context.getAttribute("username"));
System.out.println("Password: " + context.getAttribute("password"));
System.out.println("***************************************");
super.service(request, response);
}
} 当访问 PrepareConnectionServlet.action 时, 后台打印输出: ***********************************************
URL: jdbc:oracle:thin:@localhost:1521:ORC
Username: scott
Password: tigger
***********************************************

2> ActionContext
 
 ActionContext 是当前 Action 执行时的上下文环境, ActionContext 中维护了一些与当前 Action 相关的对象的引用,

如: Parameters (参数), Session (会话), ValueStack (值栈), Locale (本地化信息) 等。
 
 在 Struts1 时期, Struts1 的 Action 与 Servlet API 和 JSP 技术的耦合度都很紧密, 属于一个侵入式框架:

 public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
// TODO Auto-generated method stub
return null;
}

到了 Struts2 时期, Struts2 的体系结构与 Struts1 之间存在很大的不同。Struts2 在 Struts1 的基础上与 WebWork 进行了整合, 成为了一个全新的框架。

在 Struts2 里面, 则是通过 WebWork 来将与 Servlet 相关的数据信息转换成了与 Servlet API 无关的对象, 即 ActionContext 对象。

这样就使得了业务逻辑控制器能够与 Servlet API 分离开来。另外, 由于 Struts2 的 Action 是每一次用户请求都产生一个新的实例, 因此,

ActionContext 不存在线程安全问题, 可以放心使用。

 package net.yeah.fancydeepin.action;

 import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack; public class ContextAction extends ActionSupport { private static final long serialVersionUID = 1L;
private String username;
private String password; public String execute(){ ActionContext context = ActionContext.getContext();
ValueStack value = context.getValueStack();
value.set("username", username);
value.set("password", password);
Map<String, Object> session = context.getSession();
session.put("url", "http://www.blogjava.net/fancydeepin");
return SUCCESS;
} public void setUsername(String username) {
this.username = username;
} public void setPassword(String password) {
this.password = password;
}
} <s:property value="username"/><BR>
<s:property value="password"/><BR>
<s:property value="#session.url"/><BR>

当访问 context.action 并传给相应的参数的时候, 在浏览器中会输出相应的信息。

留意到上面 Struts2 的 Action 中并有没添加属性的 getting 方法, 而是手动的将参数值放到值栈(ValueStack)中的, 否则页面是得不到参数来输出的。

3> ServletActionContext

首先, ServletActionContext 是 ActionContext 的一个子类。ServletActionContext 从名字上来看, 意味着它与 Servlet API 紧密耦合。

ServletActionContext 的构造子是私有的, 主要是提供了一些静态的方法, 可以用来获取: ActionContext, ActionMapping, PageContext,

HttpServletRequest, HttpServletResponse, ServletContext, ValueStack, HttpSession 对象的引用。

 public String execute(){

     //或 implements ServletRequestAware
HttpServletRequest request = ServletActionContext.getRequest();
//或 implements ServletResponseAware
HttpServletResponse response = ServletActionContext.getResponse();
//或 implements SessionAware
HttpSession session = request.getSession();
//或 implements ServletContextAware
ServletContext context = ServletActionContext.getServletContext(); return SUCCESS;
}

几个 Context 上下文的区别的更多相关文章

  1. Android Context上下文解析

    1.Context概念 Context,相信不管是第一天开发Android,还是开发Android的各种老鸟,对于Context的使用一定不陌生~~你在加载资源.启动一个新的Activity.获取系统 ...

  2. Android Context 上下文 你必须知道的一切

    本文转载于:http://blog.csdn.net/lmj623565791/article/details/40481055 转载请标明出处:http://blog.csdn.net/lmj623 ...

  3. Android小项目之九 两种上下文的区别

    ------- 源自梦想.永远是你IT事业的好友.只是勇敢地说出我学到! ---------- 按惯例,写在前面的:可能在学习Android的过程中,大家会和我一样,学习过大量的基础知识,很多的知识点 ...

  4. 支持“***Context”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。

    在用VS进行MVC开发的过程中遇到如下问题: 支持“***Context”上下文的模型已在数据库创建后发生更改.请考虑使用 Code First 迁移更新数据库(http://go.microsoft ...

  5. Context 上下文

    全称:context 解释:上下文,在我们的开发的程序中,通常使用context上下文. 理解:结合实际生活我们可以把它理解为是语境,比如A说:我喜欢他. 那么这个他是谁,我们不知道,如果在这句话之前 ...

  6. Entity Framework Context上下文管理(CallContext 数据槽)

    Context上下文管理 Q1:脏数据 Q2:一次逻辑操作中,会多次访问数据库,增加了数据库服务器的压力 >在一次逻辑操作中实现上下文实例唯一 方法一:单例模式:内存的爆炸式增长 在整个运行期间 ...

  7. 有些lambda表达式就可以体现出编程中「Context(上下文)」环境

    编程中什么是「Context(上下文)」?   每一段程序都有很多外部变量.只有像Add这种简单的函数才是没有外部变量的.一旦你的一段程序有了外部变量,这段程序就不完整,不能独立运行.你为了使他们运行 ...

  8. react入门(六):状态提升&context上下文小白速懂

    一.状态提升 使用 react 经常会遇到几个组件需要共用状态数据的情况.这种情况下,我们最好将这部分共享的状态提升至他们最近的父组件当中进行管理. 原理:父组件基于属性把自己的一个fn函数传递给子组 ...

  9. Context上下文对象(抄书的)

    Servlet上下文 ServletContext 上下文接口    ServletContext接口    每一个应用都有唯一的一个上下文对象,即为ServletContext对象    Servl ...

随机推荐

  1. ssh中getHibernateTemplate()的方法

    spring接着又把业务类中的查询也封装成了find() //用来实现分页 /*HibernateTemplate ht=this.getHibernateTemplate(); DetachedCr ...

  2. Visual Studio Online Integrations-Collaboration

                                                                                         原文:http://www.v ...

  3. redhat RHEL 5.5 下载地址

    redhat RHEL 5.5 下载地址 RHEL 5 update 5 已经release许久了, redhat RHEL 5.5 下载地址: RHEL 5 安装 序列号 rhel-server-5 ...

  4. C++中构造函数详解及显式调用构造函数

    C++构造函数详解及显式调用构造函数                                         c++类的构造函数详解                        一. 构造函 ...

  5. js实现剪切、复制、粘贴——clipBoard.js

    摘要: 最近项目上要实现一个点击按钮复制链接的功能,刚开始查找了一些资料,找了几款插件,ZeroClipboard是通过flash实现的复制功能,随着越来越多的提议废除flash,于是就想能不能通过j ...

  6. 怎么让dedecms生成html页面更快些

    如何让织梦生成html页面更快些呢? 1.把文章模板里的“相关文章”.“热点文章”.“推荐文章”这类的标记删除了,用其它方式,如:shtml.js 引入 2.把织梦模板里用标记表示的模板路径.php附 ...

  7. 小希的迷宫(MST单棵树判断法则)

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  8. 黑色30s高并发IIS设置

    在这篇博文中,我们抛开对阿里云的怀疑,完全从ASP.NET的角度进行分析,看能不能找到针对问题现象的更合理的解释. “黑色30秒”问题现象的主要特征是:排队的请求(Requests Queued)突增 ...

  9. 获取并设置ListView高度的方法

    01 public void setListViewHeightBasedOnChildren(ListView listView) {  02     ListAdapter listAdapter ...

  10. Numeric Validation

    Numeric Inputs Numbers are even easier to validate than text. For number input types, the HTML5 spec ...