zoukankan      html  css  js  c++  java
  • 几个 Context 上下文的区别

    转自: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:

      1 <?xml version="1.0" encoding="UTF-8"?>
      2 
      3   <context-param>
      4     <param-name>url</param-name>
      5     <param-value>jdbc:oracle:thin:@localhost:1521:ORC</param-value>
      6   </context-param>
      7   <context-param>
      8     <param-name>username</param-name>
      9     <param-value>scott</param-value>
     10   </context-param>
     11   <context-param>
     12     <param-name>password</param-name>
     13     <param-value>tigger</param-value>
     14   </context-param>
     15   
     16   <servlet>
     17     <servlet-name>ConnectionServlet</servlet-name>
     18     <servlet-class>net.yeah.fancydeepin.servlet.ConnectionServlet</servlet-class>
     19   </servlet>
     20   <servlet-mapping>
     21     <servlet-name>ConnectionServlet</servlet-name>
     22     <url-pattern>/ConnectionServlet.action</url-pattern>
     23   </servlet-mapping>
     24   
     25   <servlet>
     26     <servlet-name>PrepareConnectionServlet</servlet-name>
     27     <servlet-class>net.yeah.fancydeepin.servlet.PrepareConnectionServlet</servlet-class>
     28   </servlet>
     29   <servlet-mapping>
     30     <servlet-name>PrepareConnectionServlet</servlet-name>
     31     <url-pattern>/PrepareConnectionServlet.action</url-pattern>
     32   </servlet-mapping>
     33 
     34 </web-app>
     35   
     36  
     37 
     38   
     39 package net.yeah.fancydeepin.servlet;
     40 
     41 import java.io.IOException;
     42 import javax.servlet.ServletContext;
     43 import javax.servlet.ServletException;
     44 import javax.servlet.http.HttpServlet;
     45 import javax.servlet.http.HttpServletRequest;
     46 import javax.servlet.http.HttpServletResponse;
     47 
     48 public class PrepareConnectionServlet extends HttpServlet {
     49 
     50     private static final long serialVersionUID = 1L;
     51 
     52     public void init() throws ServletException {
     53         
     54         ServletContext context = getServletContext();
     55         String url = context.getInitParameter("url");
     56         String username = context.getInitParameter("username");
     57         String password = context.getInitParameter("password");
     58         context.setAttribute("url", url);
     59         context.setAttribute("username", username);
     60         context.setAttribute("password", password);
     61     }
     62 
     63     protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
     64         
     65         doPost(request, response);
     66     }
     67 
     68     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     69         
     70         response.sendRedirect("ConnectionServlet.action");
     71     }
     72 }
     73 
     74   
     75  
     76 
     77   
     78 package net.yeah.fancydeepin.servlet;
     79 
     80 import java.io.IOException;
     81 import javax.servlet.ServletContext;
     82 import javax.servlet.ServletException;
     83 import javax.servlet.ServletRequest;
     84 import javax.servlet.ServletResponse;
     85 import javax.servlet.http.HttpServlet;
     86 
     87 public class ConnectionServlet extends HttpServlet {
     88 
     89     private static final long serialVersionUID = 1L;
     90 
     91     public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
     92         
     93         ServletContext context = getServletContext();
     94         System.out.println("***************************************");
     95         System.out.println("URL: " + context.getAttribute("url"));
     96         System.out.println("Username: " + context.getAttribute("username"));
     97         System.out.println("Password: " + context.getAttribute("password"));
     98         System.out.println("***************************************");
     99         super.service(request, response);
    100     }
    101 }
    102   
    103  
    104  当访问 PrepareConnectionServlet.action 时, 后台打印输出:
    105  
    106 
    107   
    108 ***********************************************
    109 URL:  jdbc:oracle:thin:@localhost:1521:ORC
    110 Username:  scott
    111 Password:  tigger
    112 ***********************************************

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

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

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

       


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

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

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

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

      
     1 package net.yeah.fancydeepin.action;
     2 
     3 import java.util.Map;
     4 import com.opensymphony.xwork2.ActionContext;
     5 import com.opensymphony.xwork2.ActionSupport;
     6 import com.opensymphony.xwork2.util.ValueStack;
     7 
     8 public class ContextAction extends ActionSupport {
     9 
    10     private static final long serialVersionUID = 1L;
    11     private String username;
    12     private String password;
    13 
    14     public String execute(){
    15         
    16         ActionContext context = ActionContext.getContext();
    17         ValueStack value = context.getValueStack();
    18         value.set("username", username);
    19         value.set("password", password);
    20         Map<String, Object> session = context.getSession();
    21         session.put("url", "http://www.blogjava.net/fancydeepin");
    22         return SUCCESS;
    23     }
    24 
    25     public void setUsername(String username) {
    26         this.username = username;
    27     }
    28 
    29     public void setPassword(String password) {
    30         this.password = password;
    31     }
    32 }
    33 
    34   
    35  
    36 
    37   
    38 <s:property value="username"/><BR>
    39 <s:property value="password"/><BR>
    40 <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 对象的引用。
     

     1 public String execute(){
     2         
     3     //或 implements ServletRequestAware
     4     HttpServletRequest request = ServletActionContext.getRequest();
     5     //或 implements ServletResponseAware
     6     HttpServletResponse response = ServletActionContext.getResponse();
     7     //或 implements SessionAware
     8     HttpSession session = request.getSession();
     9     //或 implements ServletContextAware
    10     ServletContext context = ServletActionContext.getServletContext();
    11         
    12     return SUCCESS;
    13 }


  • 相关阅读:
    26_多线程_第26天(Thread、线程创建、线程池)
    25_IO_第25天(Properties、序列化流、打印流、CommonsIO)
    24_IO_第24天(转换流、缓冲流)
    23_IO_第23天(字节流、字符流)
    22_IO_第22天(File、递归)
    PHP常用函数
    PHP后台基本语法使用笔记
    java笔记
    springmvc常用注解标签详解
    Java:全局变量(成员变量)与局部变量
  • 原文地址:https://www.cnblogs.com/x_wukong/p/3646851.html
Copyright © 2011-2022 走看看