HttpSession之學習筆記

注:此篇博文是本人看國外官方文檔得來的,建議讀者閱讀原版英文php

1.client-server connection

先上一張圖,以下java

                                     圖1web

對圖1的說明:跨域

  • client與server創建一個鏈接,這種鏈接是底層的
  • client發送request到server,等待server的answer
  • server處理request,將處理結果返還給client,這個結果包括status code、其它data

在HTTP/1.1中,在步驟3執行完成後,connection再也不被關閉,在connection有效的前提細,後面client再也不須要執行步驟1,直接執行步驟二、3就能夠。tomcat

爲了進一步深刻,以下圖2,圖2是我從國外的網上截下來的,建議讀者閱讀原文服務器

                                                圖2 HttpSession生成後會有個sessionIDcookie

  • Client第一次發送請求,web container生成惟一的session ID(生成session ID的源碼,若有興趣,能夠看下tomcat源碼),並將其返回給client(在web container返回給client的response中),web container上的這個HttpSession是臨時的。
  • 後面Client在每次發送請求給服務器時,都將session ID發送給web container,這樣web container就很容易區分出是哪一個client.
  • Web container使用這個session ID,找到對應的HttpSession,並將這次request與這個HttpSession聯繫起來。  

1.1 web container中如何得到HttpSession

    HttpServletRequest中的方法,以下圖3所示:session

/**
     *
     * Returns the current session associated with this request,
     * or if the request does not have a session, creates one.
     * 
     * @return		the <code>HttpSession</code> associated
     *			with this request
     *
     * @see	#getSession(boolean)
     *
     */

    public HttpSession getSession();

    /**
     *
     * Returns the current <code>HttpSession</code>
     * associated with this request or, if there is no
     * current session and <code>create</code> is true, returns 
     * a new session.
     *
     * <p>If <code>create</code> is <code>false</code>
     * and the request has no valid <code>HttpSession</code>,
     * this method returns <code>null</code>.
     *
     * <p>To make sure the session is properly maintained,
     * you must call this method before 
     * the response is committed. If the container is using cookies
     * to maintain session integrity and is asked to create a new session
     * when the response is committed, an IllegalStateException is thrown.
     *
     *
     *
     *
     * @param create	<code>true</code> to create
     *			a new session for this request if necessary; 
     *			<code>false</code> to return <code>null</code>
     *			if there's no current session
     *			
     *
     * @return 		the <code>HttpSession</code> associated 
     *			with this request or <code>null</code> if
     * 			<code>create</code> is <code>false</code>
     *			and the request has no valid session
     *
     * @see	#getSession()
     *
     *
     */

    public HttpSession getSession(boolean create);

                                                              圖3 獲取HttpSession的方式this

    HttpSession中的方法以下圖4所示,銷燬HttpSessionspa

/**
     * Invalidates this session then unbinds any objects bound
     * to it. 
     *
     * @exception IllegalStateException	if this method is called on an
     *					already invalidated session
     */
    public void invalidate();

                                                               圖4 銷燬HttpSession

2.client-server model缺點

    client-server model,若是client不發送請求,server不容許發送送數據給client。爲了克服這個困難,開發者能夠使用 XMLHTTPRequest請求服務器——即不斷輪詢服務器,或者WebSocket。

3.Cross-Origin Resource Sharing (CORS)    

    跨域資源共享。英文原版在這裏

相關文章
相關標籤/搜索