javaWeb使用驗證碼實現(xiàn)簡單登錄
本文實例為大家分享了javaWeb使用驗證碼實現(xiàn)簡單登錄的具體代碼,供大家參考,具體內(nèi)容如下
簡單的流程圖
1.用戶給第一次訪問login.jsp頁面時,會想服務(wù)器發(fā)送兩個請求,一個請求是顯示圖片,還有一個是顯示表單
2.第一個請求發(fā)送后 服務(wù)器中verifyServlet處理,隨機生成驗證碼圖片,并保存到session中,然后響應(yīng)給客戶端
3.第二個請求后 服務(wù)器LoginServlet處理,獲取表單驗證碼,以及session中的驗證碼,再判斷兩個驗證碼是否相等,相等就向下執(zhí)行即 success.jsp,否則跳轉(zhuǎn)到login.jsp頁面
1.創(chuàng)建login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" > <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" > --> </head> <body> <%--本頁面提供登錄表單,還有顯示錯誤信息 --%> <h1>登錄</h1> <% //讀取uname的cookie String uname=""; Cookie[] cs=request.getCookies(); if(cs!=null){ for(Cookie c:cs){ if("uname".equals(c.getName())){ uname=c.getValue(); } } } %> <% String message=""; String msg=(String)request.getAttribute("msg"); if(msg!=null){ message=msg; } %> <font color="red"><b><%=message %></b></font> <form action="/ServletDemo2/LoginServlet1" method="post"> 用戶名:<input type="text" name="username" value=<%=uname %> /><br> 密 碼:<input type="password" name="pawword" /><br> 驗證碼:<input type="text" name="verifycode" size="3"> <img src="/ServletDemo2/VerifyServlet" > <!-- <a href="javascript:_change()" rel="external nofollow" >換一張</a> --> <input type="submit" value="登錄" /> </form> </body> </html>
2.success.jsp(只是簡單的展示)
<body> <h1>succ1</h1> <% String username=(String)session.getAttribute("username"); if(username==null){ /* 向request域中保存錯誤信息,轉(zhuǎn)發(fā)到login.jsp */ request.setAttribute("msg", "請先登錄再訪問"); request.getRequestDispatcher("/session2/login.jsp").forward(request, response); return; } %> 歡迎登陸成功<%=username %> </body>
3.然后是VerifyServlet
package com.klay.servlet.session; import java.awt.image.BufferedImage; import java.io.IOException; 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 com.klay.image.VerifyCode; /** * Servlet implementation class VerifyServlet */ @WebServlet("/VerifyServlet") public class VerfiyServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { VerifyCode vc = new VerifyCode(); BufferedImage image = vc.getImage(); request.getSession().setAttribute("session_vcode", vc.getText());// 保存圖片上的文本到session VerifyCode.output(image, response.getOutputStream()); } }
4.LoginServlet
package com.klay.servlet.session; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LoginServlet extends HttpServlet { /** * @Fields serialVersionUID : TODO(用一句話描述這個變量表示什么) */ private static final long serialVersionUID = 1L; @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { /** * 獲取校驗碼 */ String sessionCode = (String) req.getSession() .getAttribute("session_vcode"); String paraCode = req.getParameter("verifycode"); if (!paraCode.equalsIgnoreCase(sessionCode)) { req.setAttribute("msg", "驗證碼錯誤"); req.getRequestDispatcher("/session2/login.jsp").forward(req, resp); // return是這里執(zhí)行完,就不玩下執(zhí)行了。 return; } /** * 獲取表單信息 */ req.setCharacterEncoding("utf-8"); String username = req.getParameter("username"); String password = req.getParameter("password"); /** * 校驗用戶名和密碼 */ if (!"kevin".equalsIgnoreCase(username))// 成功 { Cookie cookie = new Cookie("uname", username); resp.addCookie(cookie); /* * 成功就保存用戶信息到session,并重定向succ1.jsp */ HttpSession session = req.getSession(); session.setAttribute("username", username); resp.sendRedirect("/ServletDemo2/session2/succ1.jsp"); } else {// 失敗 // 如果失敗,保存錯誤信息到requset,轉(zhuǎn)發(fā)到login.jsp // (服務(wù)器內(nèi)部跳轉(zhuǎn)只有一個request請求,重定向會有新的request,就不能獲取錯誤信息) req.setAttribute("msg", "用戶名或密碼錯誤"); RequestDispatcher rd = req .getRequestDispatcher("/session2/login.jsp"); rd.forward(req, resp);// 轉(zhuǎn)發(fā) } } }
更多關(guān)于驗證碼的文章請點擊查看: 《java驗證碼》
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Java從本地文件復(fù)制到網(wǎng)絡(luò)文件上傳
這篇文章主要介紹了關(guān)于Java從本地文件復(fù)制到網(wǎng)絡(luò)文件上傳,File?和?IO?流其實是很相似的,都是將文件從一個地方轉(zhuǎn)移到另一個地方,這也是流的特點之一,需要的朋友可以參考下2023-04-04關(guān)于SpringBoot整合Canal數(shù)據(jù)同步的問題
大家都知道canal是阿里巴巴旗下的一款開源工具,純java開發(fā),支持mysql數(shù)據(jù)庫,本文給大家介紹SpringBoot整合Canal數(shù)據(jù)同步的問題,需要的朋友可以參考下2022-03-03MyBatis傳入多個參數(shù)時parameterType的寫法
這篇文章主要介紹了MyBatis傳入多個參數(shù)時parameterType的寫法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12springboot+vue實現(xiàn)七牛云頭像的上傳
本文將介紹如何在Spring Boot項目中利用七牛云進行圖片上傳并將圖片存儲在云存儲中,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08Springboot接入MyBatisPlus的實現(xiàn)
最近web端比較熱門的框架就是SpringBoot和Mybatis-Plus,這里簡單總結(jié)集成用法,具有一定的參考價值,感興趣的可以了解一下2023-09-09