<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] # 1. 會話及會話技術 ## 問題01:客戶端存儲和服務器存儲的區別? ![](https://img.kancloud.cn/70/09/70096e9ea7625fd8596aa37b8fd12f7b_663x311.png) ![](https://img.kancloud.cn/08/2b/082b8e751ec60192b5af1ebdb08ecdff_622x323.png) ## 問題02:如何寫入和讀取cookie?【記錄歷史訪問者】 ![](https://img.kancloud.cn/d2/6a/d26ad86d8bea60f81ab89588979ff636_497x274.gif) ### cookie的寫入 ``` Cookie cookie = new Cookie("name","value"); response.addCookie(cookie); ``` > **void javax.servlet.http.HttpServletResponse.addCookie(Cookie cookie)** > Adds the specified cookie to the response. This method can be called multiple times to set more than one cookie. ### cookie的讀取 ``` Cookie[] cookies = request.getCookies(); if(cookies!=null){ for(Cookie cookie:cookies){ String name = cookie.getName(); String value = cookie.getValue(); } } ``` > **Cookie[] javax.servlet.http.HttpServletRequest.getCookies()** > Returns **an array** containing all of the Cookie **objects** the client sent with this request. This method **returns null if no cookies were sent**. > **String javax.servlet.http.Cookie.getName()** > Returns the name of the cookie. The name **cannot be changed after creation**. > **String javax.servlet.http.Cookie.getValue()** > Returns the value of the cookie. > **void javax.servlet.http.Cookie.setValue(String newValue)** > Assigns **a new value** to a cookie after the cookie is created. With Version 0 cookies, values **should not contain** white space, brackets, parentheses, equals signs, commas, double quotes, slashes, question marks, at signs, colons, and semicolons. Empty values may not behave the same way on all browsers. > **(不支持特殊字符及中文)** ## 問題03:服務器存儲對象有哪些?page、request、session、application |對象名稱|所屬類型|范圍| | -- | -- | -- | |page| java.lang.Object | page| |request | javax.servlet.http.HttpServletRequest | request | |session | javax.servlet.http.HttpSession |session | | application | javax.servlet.ServletContext | application | > 為什么需要域對象? * [ ] MVC各個模塊之間需要進行相互通信,特別是JSP與Servlet、JSP與JSP、Servlet與Servlet之間 ## 問題04:為什么需要這么多存儲對象? * [ ] 適用于不同場景 * [ ] 因為request作用域太小 * [ ] 一般我們使用session,因為application作用域太大 ## 問題05:這些存儲對象有什么區別? |對象名稱|特點|應用場景| | -- | -- | -- | | page| 只在當前頁面有效 | 除當前頁面需要立即顯示數據,一般不使用 | | request | 只在當前請求有效,即相同request | 除請求轉發時,一般不使用 | | session | 一次會話有效,即沒有與服務器斷開連接 | 最常使用,非所有用戶共享數據結皆能存儲 | | application | 整個服務器運行期間有效,所有用戶共享 | 一般用于保存服務器配置信息,通常不使用 | ## 問題06:如何進行信息的增刪改查? ![](https://img.kancloud.cn/7b/ce/7bce58499a13a688e4a92f208ff92b9c_467x254.png) > **void javax.servlet.ServletRequest.setAttribute(String name, Object o)** > **Stores an attribute** in this request. **Attributes are reset** between requests. This method is most often used in conjunction with RequestDispatcher. Attribute **names** should follow **the same conventions as package names**. If the object passed in is **null**, the effect is the same as calling **removeAttribute**. --- > **void javax.servlet.ServletRequest.removeAttribute(String name)** > **Removes** an attribute from this request. --- > **Object javax.servlet.ServletRequest.getAttribute(String name)** > Returns the value of the named attribute **as an Object**, or **null** if **no** attribute of the given name **exists**. > 注意:getAttribute()因為得到的值為null或object,因此可能引發**空指針異常**或者**類型轉換異常** ## 問題07:通過request如何獲得其他存儲對象? ![](https://img.kancloud.cn/ee/fc/eefc020486873f863a9dc4f6e04821b0_755x168.png) > **HttpSession javax.servlet.http.HttpServletRequest.getSession(boolean create)** > Returns **the current HttpSession** associated with this request or, if there is **no current session** and **create is true**, returns **a new session**. If **create is false** and the request has no valid HttpSession, this method returns **null**. > **HttpSession javax.servlet.http.HttpServletRequest.getSession()** > Returns the current session associated with this request, or if the request does **not have a session, creates one**. ## 問題08:為什么不直接使用session和application,而是需要從request中得到? * [ ] 因為在Servlet中沒有內置對象,在Service方法(運行方法)中只有request參數作為的對象 ## 問題09:如何銷毀session或使session中的某一個特殊標記失效? 1. 刪除session中的屬性 ``` session.removeAttribute("key"); ``` 2. 使用空串覆蓋session中的屬性 ``` session.setAttribute("key",""); ``` 3. 設置session快速過期 ![](https://img.kancloud.cn/83/3c/833c67cc3c1ce38cd6ec9354279cf1a9_1056x291.png) ``` session.setMaxInactiveInterval?(1); ``` * [ ] 服務器統一設置過期時間 ``` <session-config> <session-timeout>min</session-timeout> </session-config> ``` 4. 使session立即失效 ![](https://img.kancloud.cn/f4/b3/f4b351aff3adc43d844702eaafcc521c_901x269.png) ``` session.invalidate(); ``` # 2. 其他內置對象page/pageContext/config/exception |對象名稱|所屬類型|范圍| | -- | -- | -- | | page | javax.servlet.jsp.HttpJspPage | page | | pageContext |javax.servlet.jsp.PageContext | page| | config |javax.servlet.ServletConfig | page| | exception|java.lang.Throwable | page| ## 問題10:pageContext有什么作用? * [ ] 獲得其他內置對象,特別是request * [ ] ``` ${pageContext.request.ContextPath} ``` ![](https://img.kancloud.cn/7e/c5/7ec5fe5cb6691bd8117dfd65c471e7ba_568x242.png) ## 問題11:是否每個JSP都有Exception對象? * [ ] 并不是 * [ ] 只有設置isErrorPage="true"才有內置對象exception ![](https://img.kancloud.cn/c8/e4/c8e48e22f6dbf022e84782830d4e337b_837x300.png) ## 問題12:九個內置對象分別屬于什么對象?即內置對象分類。 1. 錯誤對象:exception 2. servlet對象:page、config 3. 輸入輸出對象:request、response、out 4. 通信對象:session、application、pageContext
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看