您好,登錄后才能下訂單哦!
本文實例為大家分享了javaWeb使用驗證碼實現簡單登錄的具體代碼,供大家參考,具體內容如下
簡單的流程圖
1.用戶給第一次訪問login.jsp頁面時,會想服務器發送兩個請求,一個請求是顯示圖片,還有一個是顯示表單
2.第一個請求發送后 服務器中verifyServlet處理,隨機生成驗證碼圖片,并保存到session中,然后響應給客戶端
3.第二個請求后 服務器LoginServlet處理,獲取表單驗證碼,以及session中的驗證碼,再判斷兩個驗證碼是否相等,相等就向下執行即 success.jsp,否則跳轉到login.jsp頁面
1.創建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> <%--本頁面提供登錄表單,還有顯示錯誤信息 --%> <h2>登錄</h2> <% //讀取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> <h2>succ1</h2> <% String username=(String)session.getAttribute("username"); if(username==null){ /* 向request域中保存錯誤信息,轉發到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是這里執行完,就不玩下執行了。 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,轉發到login.jsp // (服務器內部跳轉只有一個request請求,重定向會有新的request,就不能獲取錯誤信息) req.setAttribute("msg", "用戶名或密碼錯誤"); RequestDispatcher rd = req .getRequestDispatcher("/session2/login.jsp"); rd.forward(req, resp);// 轉發 } } }
更多關于驗證碼的文章請點擊查看: 《java驗證碼》
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。