jsp网页登陆验证

jsp登陆验证,网页登陆验证带验证码校验,登录功能之添加验证码

part_1:专门用于生成一个验证码图片的类:VerificationCode.java

package cn.mike.javase.test; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Random; import javax.imageio.ImageIO; import org.junit.Test; /** * @author : Administrator * @function : 这是用来测试随机生成验证码图片的类; */ public class VerificationCode { /** * 单元测试,试一下能不能自动生成验证码图片 */ // 这个函数是单元测试时使用的,这里private一下外面就调用不到了; /* @Test */ /* public */private void test_fun() { VerificationCode vc = new VerificationCode(); BufferedImage image = vc.getImage(); try { // 生成验证码图片,并保存到指定的路径 VerificationCode.output(image, new FileOutputStream(new File( ".\\image\\vcode_2.jpg"))); } catch (FileNotFoundException e) { e.printStackTrace(); } // 将随机生成的文本内容输出到控制台,用于校验 System.out.println(vc.getText()); } private int w = 70;// 宽 private int h = 35;// 高 private String text;// 文本内容(验证码字符串) private Random r = new Random(); private String[] fontNames = { "宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312" }; // 随机字符集合中不包括0和o,O,1和l,因为这些不易区分 private String codes = "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYXZ"; // 验证码图片的背景色:白色 private Color bgColor = new Color(255, 255, 255); /** * 返回一个验证码图片buffer对象:BufferedImage */ public BufferedImage getImage() { BufferedImage image = createImage(); // 获取绘图环境(画笔工具) Graphics2D g2 = (Graphics2D) image.getGraphics(); // sb : 用来保存验证码字符串文本内容 StringBuilder sb = new StringBuilder(); for (int i = 0; i < 4; ++i) {// 随机生成4个字符 String s = randomChar() + ""; sb.append(s); float x = i * 1.0F * w / 4; g2.setFont(randomFont()); g2.setColor(randomColor()); g2.drawString(s, x, h - 5); } this.text = sb.toString();// 记录验证码文本内容 drawLine(image);// 画干扰线 return image; } /** * @return 获取验证码文本内容 */ public String getText() { return text; } /** * @param image * @param out * 将文本写到指定的输出流。比如本测试中FileOutputStream指定的保存路径 */ public static void output(BufferedImage image, OutputStream out) { try { ImageIO.write(image, "jpeg", out); } catch (IOException e) { e.printStackTrace(); } } private void drawLine(BufferedImage image) { Graphics2D g2 = (Graphics2D) image.getGraphics(); for (int i = 0; i < 3; ++i) {// 画3条干扰线 int x1 = r.nextInt(w); int y1 = r.nextInt(h); int x2 = r.nextInt(w); int y2 = r.nextInt(h); g2.setColor(Color.BLUE); g2.drawLine(x1, y1, x2, y2); } } private Color randomColor() { int red = r.nextInt(150); int green = r.nextInt(150); int blue = r.nextInt(150); return new Color(red, green, blue); } private Font randomFont() { int index = r.nextInt(fontNames.length); String fontName = fontNames[index]; int style = r.nextInt(4); int size = r.nextInt(5) + 24; return new Font(fontName, style, size); } private char randomChar() { int index = r.nextInt(codes.length()); return codes.charAt(index); } private BufferedImage createImage() { BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = (Graphics2D) image.getGraphics(); g2.setColor(this.bgColor); g2.fillRect(0, 0, w, h); return image; } }

part_2:登录界面:Login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "https://www.jb51.net/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="https://www.jb51.net/<%=basePath%>"> <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 type="text/css" href="https://www.jb51.net/styles.css"> --> <script type="text/javascript"> function _change_verity_code() { var imgElem = document.getElementById("img_src"); //添加一个请求参数a是因为,通常浏览器都有缓存,点击换一张的时候没反应,所以加一个请求参数,获取当前请求时间,可以精确到毫秒,所以每次请求的参数都不同,所以浏览器有缓存也不妨碍; imgElem.src = "/ServletDemoProject/servlet/GetVerificationCodeServlet?a=" + new Date().getTime(); } </script> </head> <% String fdbkMsg = (String) request.getAttribute("fdbkMsg"); if (null == fdbkMsg) { fdbkMsg = ""; } %> <% Boolean logedIn = (Boolean) session.getAttribute("logedIn"); if (null == logedIn) { logedIn = false; } else if (logedIn) { //如果在本次会话已经登陆,直接重定向到success-page-1 response .sendRedirect("/ServletDemoProject/LOGIN-DEMO/success-page-1.jsp"); } %> <% String username = ""; Cookie[] cookies = request.getCookies(); if ((null != cookies) && (cookies.length > 0)) { for (Cookie c : cookies) { if ("admin".equals(c.getValue())) { username = "admin"; break; } } }//end if-condition %> <body> <br> <div> 请登录: <br> <form action="/ServletDemoProject/servlet/LoginVerificationServlet" method="post"> <div> 用户名: <input type="text" value="<%=username%>" /> <br> </div> <div> 密 码: <input type="password" /> <br> </div> <div> 验证码: <input type="text" size="3" /> <img src="https://www.jb51.net/ServletDemoProject/servlet/GetVerificationCodeServlet" /> <a href="javascript:_change_verity_code()">换一张</a> <br> </div> <div> <font color='red'><%=fdbkMsg%></font> <br> </div> <div> <input type="submit" value="提交" /> <br> </div> </form> </div> </body> </html>

part_3:处理登录校验的servlet :LoginVerificationServlet.java

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzjxdw.html