几个 Context 上下文的区别

在 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;

}

转自:http://www.blogjava.net/fancydeepin

全部评论

相关推荐

老粉都知道小猪猪我很久没更新了,因为秋招非常非常不顺利,emo了三个月了,接下来说一下我的情况吧本人是双非本&nbsp;专业是完全不着计算机边的非科班,比较有优势的是有两段大厂实习,美团和字节。秋招面了50+场泡池子泡死的:滴滴&nbsp;快手&nbsp;去哪儿&nbsp;小鹏汽车&nbsp;不知名的一两个小厂其中字节13场&nbsp;两次3面挂&nbsp;两次2面挂&nbsp;一次一面挂其中有2场面试题没写出来,其他的都是全a,但该挂还是挂,第三次三面才面进去字节,秋招加暑期总共面了22次字节,在字节的面评可以出成书了快手面了8场,2次实习的,通过了但没去,一次2面挂&nbsp;最后一次到录用评估&nbsp;至今无消息滴滴三面完&nbsp;没几天挂了&nbsp;所有技术面找不出2个问题是我回答不上来的,三面还来说我去过字节,应该不会考虑滴滴吧,直接给我干傻了去哪儿一天速通&nbsp;至今无消息小鹏汽车hr&nbsp;至今无消息美团2面挂&nbsp;然后不捞我了,三个志愿全部结束,估计被卡学历了虾皮二面挂&nbsp;这个是我菜,面试官太牛逼了拼多多二面挂&nbsp;3道题也全写了&nbsp;也没问题是回答不出来的&nbsp;泡一周后挂腾讯面了5次&nbsp;一次2面挂&nbsp;三次一面挂,我宣布腾讯是世界上最难进的互联网公司然后还有一些零零散散的中小厂,但是数量比较少,约面大多数都是大厂。整体的战况非常惨烈,面试机会少,就算面过了也需要和各路神仙横向对比,很多次我都是那个被比下去的人,不过这也正常,毕竟谁会放着一个985的硕士不招,反而去招一个双非读化学的小子感觉现在互联网对学历的要求越来越高了,不仅仅要985还要硕士了,双非几乎没啥生存空间了,我感觉未来几年双非想要进大厂开发的难度应该直线上升了,唯一的打法还是从大二刷实习,然后苟个转正,不然要是去秋招大概率是炮灰。而且就我面字节这么多次,已经开始问很多ai的东西了,你一破本科生要是没实习没科研懂什么ai啊,纯纯白给了
不知名牛友_:爸爸
秋招你被哪家公司挂了?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务