首页 > 代码库 > jsp验证码
jsp验证码
1、validimg.jsp
1 <%@ page contentType="image/JPEG" 2 import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" 3 pageEncoding="UTF-8"%> 4 <%! 5 //给定范围获得随机颜色 6 private Color getRandColor(int fc, int bc) { 7 Random random = new Random(); 8 if (fc > 255) 9 fc = 255; 10 if (bc > 255) 11 bc = 255; 12 int r = fc + random.nextInt(bc - fc); 13 int g = fc + random.nextInt(bc - fc); 14 int b = fc + random.nextInt(bc - fc); 15 return new Color(r, g, b); 16 } 17 %> 18 <% 19 //设置页面不缓存 20 response.setHeader("Pragma", "No-cache"); 21 response.setHeader("Cache-Control", "no-cache"); 22 response.setDateHeader("Expires", 0); 23 24 // 在内存中创建图象 25 int width = 60, height = 20; 26 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 27 28 // 获取图形上下文 29 Graphics g = image.getGraphics(); 30 31 // 设定背景色 32 g.setColor(this.getRandColor(200, 250)); 33 g.fillRect(0, 0, width, height); 34 35 //设定字体 36 g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); 37 38 //生成随机类 39 Random random = new Random(); 40 41 // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到 42 g.setColor(this.getRandColor(160, 200)); 43 for (int i = 0; i < 100; i++) { 44 int x = random.nextInt(width); 45 int y = random.nextInt(height); 46 int xl = random.nextInt(12); 47 int yl = random.nextInt(12); 48 g.drawLine(x, y, x + xl, y + yl); 49 } 50 51 // 取随机产生的认证码(4位数字) 52 String validCode = ""; 53 for (int i = 0; i < 4; i++) { 54 String rand = String.valueOf(random.nextInt(10)); 55 validCode += rand; 56 // 将认证码显示到图象中 57 g.setColor(new Color(20 + random.nextInt(110), 20 + random 58 .nextInt(110), 20 + random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成 59 g.drawString(rand, 13 * i + 6, 16); 60 } 61 62 // 将认证码存入SESSION 63 session.setAttribute("validCode", validCode); 64 65 // 图象生效 66 g.dispose(); 67 68 // 输出图象到页面 69 ImageIO.write(image, "JPEG", response.getOutputStream()); 70 %>
2、login.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 <title>登录页面</title> 6 7 </head> 8 9 <body> 10 <h1>登录页面</h1> 11 <form action="logindo.jsp" method="post"> 12 <table> 13 <tr> 14 <td><label for="loginName">登录姓名:</label></td> 15 <td><input type="text" name="loginName" id="loginName"/></td> 16 </tr> 17 <tr> 18 <td><label for="loginPwd">登录密码:</label></td> 19 <td><input type="password" name="loginPwd" id="loginPwd"/></td> 20 </tr> 21 <tr> 22 <td><label for="validCode">验证码:</label></td> 23 <td><input type="text" maxlength="4" style="width:60px" name="validCode" id="validCode"/><img src="validimg.jsp" alt="验证码"/></td> 24 </tr> 25 <tr> 26 <td colspan="2" align="center"> 27 <input type="submit" value="登录"/> 28 <input type="reset" value="重填"/> 29 </td> 30 </tr> 31 </table> 32 </form> 33 </body> 34 </html>
3、logindo.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@page import="entity.UserInfo"%> 3 <%@page import="dao.UserInfoDao"%> 4 <% 5 //获取表单内容 6 String validCode = request.getParameter("validCode"); 7 String sValidCode = (String) session.getAttribute("validCode"); 8 if (validCode.equals(sValidCode)) { 9 String loginName = request.getParameter("loginName"); 10 String loginPwd = request.getParameter("loginPwd"); 11 12 UserInfo userInfo = UserInfoDao.instance().Login(loginName,loginPwd); 13 14 if (userInfo != null) { 15 if (loginName.equals(userInfo.getLoginName())) { 16 session.setAttribute("login",userInfo); 17 response.sendRedirect("manager.jsp"); 18 } else { 19 out.print("登录失败!"); 20 } 21 } else { 22 out.print("登录失败!"); 23 } 24 } else { 25 out.print("验证码失败!"); 26 } 27 %>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。