<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-126  評論-247  文章-5  trackbacks-0

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

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

     那么, Context 究竟是什么東西呢? 直譯是上下文、環(huán)境的意思。比如像: "今天我收到了一束花, 男朋友送的!" 又或者 "今天我收到了一束花, 送花的人送錯了的!"

     同樣是收到一束花, 在不同的上下文環(huán)境中表達的意義是不一樣的。

     同樣的, Context 其實也是一樣, 它離不開所在的上下文環(huán)境, 否則就是斷章取義了。

     另外, 在網絡上也有些人把 Context 看成是一些公用信息或者把它看做是一個容器的, 個人覺得這種解釋稍好。

     接下來說說 ServletContext, ActionContext, ServletActionContext
     
     1> ServletContext

     一個 WEB 運用程序只有一個 ServletContext 實例, 它是在容器(包括 JBoss, Tomcat 等)完全啟動 WEB 項目之前被創(chuàng)建, 生命周期伴隨整個 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 執(zhí)行時的上下文環(huán)境, 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 對象。

     這樣就使得了業(yè)務邏輯控制器能夠與 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.tkk7.com/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 的構造子是私有的, 主要是提供了一些靜態(tài)的方法, 可以用來獲取: 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;
    }
      


     
     
     



      
    posted on 2013-03-31 15:30 fancydeepin 閱讀(20649) 評論(6)  編輯  收藏

    評論:
    # re: 幾個 Context 上下文的區(qū)別[未登錄] 2013-12-04 10:47 | kyle
    作者總結性極強,幫助很大,多謝了~  回復  更多評論
      
    # re: 幾個 Context 上下文的區(qū)別[未登錄] 2014-02-28 12:01 | cxh
    作者寫得很好,對我初學者很好幫助,謝謝  回復  更多評論
      
    # re: 幾個 Context 上下文的區(qū)別[未登錄] 2015-03-27 16:12 | windy
    多給幾個贊!  回復  更多評論
      
    # re: 幾個 Context 上下文的區(qū)別[未登錄] 2015-07-30 15:27 | windy
    頂  回復  更多評論
      
    # re: 幾個 Context 上下文的區(qū)別 2015-08-07 17:16 | 編程小王子
    是不是還有后續(xù)幾個的???  回復  更多評論
      
    # re: 幾個 Context 上下文的區(qū)別 2016-03-11 13:28 | 溫柔次
    請3534起特特愛聽  回復  更多評論
      

    只有注冊用戶登錄后才能發(fā)表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲精品国产首次亮相| 亚洲中字慕日产2020| 人人爽人人爽人人片A免费| 曰皮全部过程视频免费国产30分钟| 亚洲av永久无码精品天堂久久| 最近免费中文字幕大全免费 | 亚洲av综合avav中文| a级毛片在线视频免费观看| 日韩亚洲一区二区三区| 中文无码成人免费视频在线观看| 国产V亚洲V天堂A无码| 亚洲午夜免费视频| 亚洲日本久久久午夜精品| 国产亚洲高清不卡在线观看| 中文字幕a∨在线乱码免费看| 国产亚洲综合色就色| 久久99国产乱子伦精品免费| 亚洲成人免费网址| 国产中文字幕免费| 中文字幕a∨在线乱码免费看| 亚洲精品福利视频| 成人毛片免费观看| caoporm超免费公开视频| 亚洲AV无码专区在线播放中文| 四虎免费影院ww4164h| 国产AV无码专区亚洲AV蜜芽| 中文字幕精品无码亚洲字| 久久这里只精品热免费99| 久久亚洲国产成人影院| 亚洲日韩VA无码中文字幕| 在线观看免费中文视频| 亚洲国产无线乱码在线观看| 亚洲综合无码精品一区二区三区| 6080午夜一级毛片免费看 | 亚洲人午夜射精精品日韩| 99久久国产免费-99久久国产免费| 亚洲最大成人网色香蕉| AV在线播放日韩亚洲欧| 在线永久看片免费的视频| 无套内射无矿码免费看黄| 久久精品国产亚洲av日韩|