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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Servlet 中的`HttpSession` > 原文: [https://beginnersbook.com/2013/05/http-session/](https://beginnersbook.com/2013/05/http-session/) `HttpSession`對象用于會話管理。會話包含特定于整個應用中的特定用戶的信息。當用戶第一次通過`request.getSession()`獲得`HttpSession`時進入網站(或在線應用)時,將為用戶提供唯一 ID 以標識其會話。此唯一 ID 可以存儲在 cookie 或請求參數中。 `HttpSession`保持活動狀態,直到它的使用時間超過部署描述符文件(`web.xml`)中標簽中指定的超時值。默認超時值為 30 分鐘,如果未在標記中指定值,則使用此值。這意味著當用戶未訪問指定的 Web 應用時間時,會話將被 servlet 容器銷毀。后續請求將不再從此會話提供,servlet 容器將創建新會話。 這是您創建`HttpSession`對象的方法。 ```java protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { HttpSession session = req.getSession(); } ``` 您可以使用`setAttribute()`方法將用戶信息存儲到會話對象中,稍后在需要時可以從會話中獲取此信息。這是您在會話中存儲信息的方式。這里我們分別在屬性名稱為`uName`,`uemailId`和`uAge`的會話中存儲用戶名,`emailid`和`userage`。 ```java session.setAttribute("uName", "ChaitanyaSingh"); session.setAttribute("uemailId", "[email?protected]"); session.setAttribute("uAge", "30"); ``` 第一個參數是屬性名稱,第二個是屬性值。對于例如`uName`是屬性名稱,`ChaitanyaSingh`是上面代碼中的屬性值。 要從會話中獲取值,我們使用`HttpSession`接口的`getAttribute()`方法。這里我們使用屬性名稱獲取屬性值。 ```java String userName = (String) session.getAttribute("uName"); String userEmailId = (String) session.getAttribute("uemailId"); String userAge = (String) session.getAttribute("uAge"); ``` ## `HttpSession`的方法 `public void setAttribute(String name,Object value)`:使用名稱綁定對象,并將名稱/值對存儲為 HttpSession 對象的屬性。如果屬性已存在,則此方法將替換現有屬性。 `public Object getAttribute(String name)`:從會話對象返回參數中指定的`String`對象。如果未找到指定屬性的對象,則`getAttribute()`方法返回`null`。 `public Enumeration getAttributeNames()`:返回一個`Enumeration`,其中包含作為會話對象的屬性綁定的所有對象的名稱。 `public void removeAttribute(String name)`:從會話中刪除給定的屬性。 `setMaxInactiveInterval(int interval)`:以秒為單位設置會話不活動時間。這是以秒為單位的時間,指定自客戶端上次收到請求后會話保持活動狀態的時間。 有關方法的完整列表,請參閱[官方文檔](https://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpSession.html)。 ## 會話示例 `index.html` ```java <form action="login"> User Name:<input type="text" name="userName"/><br/> Password:<input type="password" name="userPassword"/><br/> <input type="submit" value="submit"/> </form> ``` `MyServlet1.java` ```java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class MyServlet1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response){ try{ response.setContentType("text/html"); PrintWriter pwriter = response.getWriter(); String name = request.getParameter("userName"); String password = request.getParameter("userPassword"); pwriter.print("Hello "+name); pwriter.print("Your Password is: "+password); HttpSession session=request.getSession(); session.setAttribute("uname",name); session.setAttribute("upass",password); pwriter.print("<a href='welcome'>view details</a>"); pwriter.close(); }catch(Exception exp){ System.out.println(exp); } } } ``` `MyServlet2.java` ```java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class MyServlet2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response){ try{ response.setContentType("text/html"); PrintWriter pwriter = response.getWriter(); HttpSession session=request.getSession(false); String myName=(String)session.getAttribute("uname"); String myPass=(String)session.getAttribute("upass"); pwriter.print("Name: "+myName+" Pass: "+myPass); pwriter.close(); }catch(Exception exp){ System.out.println(exp); } } } ``` `web.xml` ```java <web-app> <servlet> <servlet-name>Servlet1</servlet-name> <servlet-class>MyServlet1</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet1</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <servlet> <servlet-name>Servlet2</servlet-name> <servlet-class>MyServlet2</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet2</servlet-name> <url-pattern>/welcome</url-pattern> </servlet-mapping> </web-app> ``` **輸出:** **第一個界面:** ![](https://img.kancloud.cn/f0/c5/f0c57072c9754074a55a46ea09c7c26d_700x137.jpg) **點擊提交后:** ![](https://img.kancloud.cn/dc/df/dcdf881d68bba70b2e73c1566cb9de3e_700x82.jpg) **點擊查看詳細信息后:** ![](https://img.kancloud.cn/65/0d/650d67608abcf0b82eff82478c691765_700x114.jpg)
                  <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>

                              哎呀哎呀视频在线观看