<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之旅 廣告
                **1.登錄案例** ![](http://h.yiniuedu.com/c0e37f8aab09abe2c6963bc39dee861b) ## 1.Cookie案例_記住上次訪問時間 案例需求: >1. 訪問一個Servlet,如果是第一次訪問,則提示:您好,歡迎您首次訪問。 > 2. 如果不是第一次訪問,則提示:歡迎回來,您上次訪問時間為:顯示時間字符串 ## 2.Session案例_驗證碼 案例需求: >1. 訪問帶有驗證碼的登錄頁面login.jsp >2. 用戶輸入用戶名,密碼以及驗證碼。 >>* 如果用戶名和密碼輸入有誤,跳轉登錄頁面,提示:用戶名或密碼錯誤 >>* 如果驗證碼輸入有誤,跳轉登錄頁面,提示:驗證碼錯誤 >>* 如果全部輸入正確,則跳轉到主頁success.jsp,顯示:用戶名,歡迎您 login.jsp界面代碼如下: ``` <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>login</title> <script> window.onload = function(){ document.getElementById("img").onclick = function(){ this.src="CheckCode?time="+new Date().getTime(); } } </script> <style> div{ color: red; } </style> </head> <body> <form action="loginServlet" method="post"> <table> <tr> <td>用戶名</td> <td><input type="text" name="username"></td> </tr> <tr> <td>密碼</td> <td><input type="password" name="password"></td> </tr> <tr> <td>驗證碼</td> <td><input type="text" name="checkCode"></td> </tr> <tr> <td colspan="2"><img id="img" src="CheckCode"></td> </tr> <tr> <td colspan="2"><input type="submit" value="登錄"></td> </tr> </table> </form> <div><%=request.getAttribute("cc_error") == null ? "" : request.getAttribute("cc_error")%></div> <div><%=request.getAttribute("login_error") == null ? "" : request.getAttribute("login_error") %></div> </body> </html> ``` success.jsp界面代碼如下: ``` <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1><%=request.getSession().getAttribute("user")%>,歡迎您</h1> </body> </html> ``` LoginServlet.java代碼如下: ``` import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; @WebServlet("/loginServlet") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.設置request編碼 request.setCharacterEncoding("utf-8"); //2.獲取參數 String username = request.getParameter("username"); String password = request.getParameter("password"); String checkCode = request.getParameter("checkCode"); //3.先獲取生成的驗證碼 HttpSession session = request.getSession(); String checkCode_session = (String) session.getAttribute("checkcode"); //刪除session中存儲的驗證碼 session.removeAttribute("checkcode"); //3.先判斷驗證碼是否正確 if(checkCode_session!= null && checkCode_session.equalsIgnoreCase(checkCode)){ //忽略大小寫比較 //驗證碼正確 //判斷用戶名和密碼是否一致 if("zhangsan".equals(username) && "123".equals(password)){//需要調用UserDao查詢數據庫 //登錄成功 //存儲信息,用戶信息 session.setAttribute("user",username); //重定向到success.jsp response.sendRedirect(request.getContextPath()+"/success.jsp"); }else{ //登錄失敗 //存儲提示信息到request request.setAttribute("login_error","用戶名或密碼錯誤"); //轉發到登錄頁面 request.getRequestDispatcher("/login.jsp").forward(request,response); } }else{ //驗證碼不一致 //存儲提示信息到request request.setAttribute("cc_error","驗證碼錯誤"); //轉發到登錄頁面 request.getRequestDispatcher("/login.jsp").forward(request,response); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } } ``` ## 3.課后任務: 1.通過cookie記住上次訪問時間 2.通過session完成驗證碼校驗
                  <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>

                              哎呀哎呀视频在线观看