Session-判断用户登陆验证码是否正确
生活随笔
收集整理的這篇文章主要介紹了
Session-判断用户登陆验证码是否正确
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
個人博客地址https://nfreak-man.cn
驗證碼為隨機生成,服務器從 session 獲取驗證碼,并和用戶輸入的驗證碼進行比對,結果通過 requesrt 轉發到 success.jsp 和 login.jsp
login.jsp
簡單登陸頁面:
<html> <head><title>登陸</title><script>window.onload = function () {document.getElementById("img").onclick = function(){this.src="/day16/checkCodeServlet?time="+new Date().getTime();}}</script><style>div{color:red;}</style> </head> <body><form action="/day16/loginServlet" method="post"><table align="center"><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 src="/day16/checkCodeServlet" id="img"></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
登陸成功后跳轉到該頁面,并獲取用戶信息展示:
<html> <head><title>Title</title> </head> <body><h1><%=request.getSession().getAttribute("user") %>,歡迎您</h1> </body> </html>LoginServlet
獲取 session 中驗證碼信息,進行比對,并轉發結果到相關頁面。
@WebServlet("/loginServlet") public class LoginServlet extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//設置request編碼request.setCharacterEncoding("utf-8");//獲取參數String username = request.getParameter("username");String password = request.getParameter("password");String checkCode = request.getParameter("checkCode");//獲取生成的驗證碼HttpSession session = request.getSession();String checkCode_session = (String) session.getAttribute("checkCode_session");//刪除session中存儲的驗證碼session.removeAttribute("checkCode_session");//判斷驗證碼是否正確if(checkCode_session != null &&checkCode_session.equalsIgnoreCase(checkCode)){//忽略大小寫比較字符串//驗證碼正確//判斷用戶名和密碼是否一樣if ("zhangsan".equals(username)&&"123".equals(password)){//查詢數據庫//登陸成功//存儲用戶信息session.setAttribute("user",username);//重定向到success.jspresponse.sendRedirect(request.getContextPath()+"/success.jsp");}else {//登陸失敗//存儲提示信息到requestrequest.setAttribute("login_error","用戶名或密碼錯誤");//轉發到登陸頁面request.getRequestDispatcher("/login.jsp").forward(request,response);}}else {//驗證碼不一致//存儲提示信息到requestrequest.setAttribute("cc_error","驗證碼錯誤");//轉發到登陸頁面request.getRequestDispatcher("/login.jsp").forward(request,response);}}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request, response);} }CheckCodeServlet
生成隨機驗證碼,并存入 session 。
@WebServlet("/checkCodeServlet") public class CheckCodeServlet extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {int width = 100;int height = 50;//創建一個對象,在內存中畫圖(驗證碼圖片對象)BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);//美化圖片//填充背景色Graphics g = image.getGraphics();//畫筆對象g.setColor(Color.pink);//設置畫筆顏色g.fillRect(0,0,width,height);//畫邊框g.setColor(Color.BLUE);g.drawRect(0,0,width -1,height-1);String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";//生成隨機腳標Random ran = new Random();StringBuilder sb = new StringBuilder();//寫驗證碼for (int i = 1; i <= 4; i++) {int index = ran.nextInt(str.length());//獲取字符char ch = str.charAt(index);sb.append(ch);g.drawString(ch+"",width/5*i,height/2);}String checkCode_session = sb.toString();//將驗證碼存入sessionrequest.getSession().setAttribute("checkCode_session",checkCode_session);//畫干擾線g.setColor(Color.green);//隨機生成坐標點for (int i = 0; i < 10; i++) {int x1 = ran.nextInt(width);int x2 = ran.nextInt(width);int y1 = ran.nextInt(height);int y2 = ran.nextInt(height);g.drawLine(x1,y1,x2,y2);}//鍵土拍你輸出到頁面展示ImageIO.write(image,"jpg",response.getOutputStream());}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request,response);} }總結
以上是生活随笔為你收集整理的Session-判断用户登陆验证码是否正确的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1、vimdiff
- 下一篇: 常见搜索算法(二):二分查找