javaWeb使用驗(yàn)證碼實(shí)現(xiàn)簡(jiǎn)單登錄
本文實(shí)例為大家分享了javaWeb使用驗(yàn)證碼實(shí)現(xiàn)簡(jiǎn)單登錄的具體代碼,供大家參考,具體內(nèi)容如下
簡(jiǎn)單的流程圖
1.用戶(hù)給第一次訪問(wèn)login.jsp頁(yè)面時(shí),會(huì)想服務(wù)器發(fā)送兩個(gè)請(qǐng)求,一個(gè)請(qǐng)求是顯示圖片,還有一個(gè)是顯示表單
2.第一個(gè)請(qǐng)求發(fā)送后 服務(wù)器中verifyServlet處理,隨機(jī)生成驗(yàn)證碼圖片,并保存到session中,然后響應(yīng)給客戶(hù)端
3.第二個(gè)請(qǐng)求后 服務(wù)器LoginServlet處理,獲取表單驗(yàn)證碼,以及session中的驗(yàn)證碼,再判斷兩個(gè)驗(yàn)證碼是否相等,相等就向下執(zhí)行即 success.jsp,否則跳轉(zhuǎn)到login.jsp頁(yè)面
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> <%--本頁(yè)面提供登錄表單,還有顯示錯(cuò)誤信息 --%> <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"> 用戶(hù)名:<input type="text" name="username" value=<%=uname %> /><br> 密 碼:<input type="password" name="pawword" /><br> 驗(yàn)證碼:<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(只是簡(jiǎn)單的展示)
<body> <h1>succ1</h1> <% String username=(String)session.getAttribute("username"); if(username==null){ /* 向request域中保存錯(cuò)誤信息,轉(zhuǎn)發(fā)到login.jsp */ request.setAttribute("msg", "請(qǐng)先登錄再訪問(wèn)"); 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(用一句話(huà)描述這個(gè)變量表示什么) */ private static final long serialVersionUID = 1L; @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { /** * 獲取校驗(yàn)碼 */ String sessionCode = (String) req.getSession() .getAttribute("session_vcode"); String paraCode = req.getParameter("verifycode"); if (!paraCode.equalsIgnoreCase(sessionCode)) { req.setAttribute("msg", "驗(yàn)證碼錯(cuò)誤"); 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"); /** * 校驗(yàn)用戶(hù)名和密碼 */ if (!"kevin".equalsIgnoreCase(username))// 成功 { Cookie cookie = new Cookie("uname", username); resp.addCookie(cookie); /* * 成功就保存用戶(hù)信息到session,并重定向succ1.jsp */ HttpSession session = req.getSession(); session.setAttribute("username", username); resp.sendRedirect("/ServletDemo2/session2/succ1.jsp"); } else {// 失敗 // 如果失敗,保存錯(cuò)誤信息到requset,轉(zhuǎn)發(fā)到login.jsp // (服務(wù)器內(nèi)部跳轉(zhuǎn)只有一個(gè)request請(qǐng)求,重定向會(huì)有新的request,就不能獲取錯(cuò)誤信息) req.setAttribute("msg", "用戶(hù)名或密碼錯(cuò)誤"); RequestDispatcher rd = req .getRequestDispatcher("/session2/login.jsp"); rd.forward(req, resp);// 轉(zhuǎn)發(fā) } } }
更多關(guān)于驗(yàn)證碼的文章請(qǐng)點(diǎn)擊查看: 《java驗(yàn)證碼》
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Java從本地文件復(fù)制到網(wǎng)絡(luò)文件上傳
這篇文章主要介紹了關(guān)于Java從本地文件復(fù)制到網(wǎng)絡(luò)文件上傳,File?和?IO?流其實(shí)是很相似的,都是將文件從一個(gè)地方轉(zhuǎn)移到另一個(gè)地方,這也是流的特點(diǎn)之一,需要的朋友可以參考下2023-04-04關(guān)于SpringBoot整合Canal數(shù)據(jù)同步的問(wèn)題
大家都知道canal是阿里巴巴旗下的一款開(kāi)源工具,純java開(kāi)發(fā),支持mysql數(shù)據(jù)庫(kù),本文給大家介紹SpringBoot整合Canal數(shù)據(jù)同步的問(wèn)題,需要的朋友可以參考下2022-03-03MyBatis傳入多個(gè)參數(shù)時(shí)parameterType的寫(xiě)法
這篇文章主要介紹了MyBatis傳入多個(gè)參數(shù)時(shí)parameterType的寫(xiě)法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12MyBatis-Plus 插件擴(kuò)展的實(shí)現(xiàn)
MyBatis-Plus通過(guò)插件擴(kuò)展機(jī)制增強(qiáng)功能,基于MyBatis Interceptor攔截器,包括分頁(yè)插件、邏輯刪除、SQL性能分析和樂(lè)觀鎖等,開(kāi)發(fā)者可自定義插件以適應(yīng)特定需求,有效地增強(qiáng)SQL執(zhí)行過(guò)程的控制和優(yōu)化,同時(shí)注意插件使用的性能影響和執(zhí)行順序2024-09-09springboot+vue實(shí)現(xiàn)七牛云頭像的上傳
本文將介紹如何在Spring Boot項(xiàng)目中利用七牛云進(jìn)行圖片上傳并將圖片存儲(chǔ)在云存儲(chǔ)中,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08Springboot接入MyBatisPlus的實(shí)現(xiàn)
最近web端比較熱門(mén)的框架就是SpringBoot和Mybatis-Plus,這里簡(jiǎn)單總結(jié)集成用法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09