DDR爱好者之家 Design By 杰米
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 + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=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 rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript"> function _change_verity_code() { var imgElem = document.getElementById("img_src"); //添加一个请求参数a是因为,通常浏览器都有缓存,点击换一张的时候没反应,所以加一个请求参数,获取当前请求时间,可以精确到毫秒,所以每次请求的参数都不同,所以浏览器有缓存也不妨碍; imgElem.src = "/ServletDemoProject/servlet/GetVerificationCodeServlet" + 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 align="center"> 请登录: <br> <form action="/ServletDemoProject/servlet/LoginVerificationServlet" method="post"> <div> 用户名: <input type="text" name="username" value="<%=username%>" /> <br> </div> <div> 密 码: <input type="password" name="password" /> <br> </div> <div> 验证码: <input type="text" name="code_text" size="3" /> <img src="/UploadFiles/2021-04-02/GetVerificationCodeServlet">part_3:处理登录校验的servlet :LoginVerificationServlet.java
package cn.mike.servlet.test_1212; import java.awt.image.BufferedImage; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.mike.javase.test.VerificationCode; public class GetVerificationCodeServlet extends HttpServlet { private static final long serialVersionUID = -3520994675366100452L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1.新建一个VerificationCode类; VerificationCode vc = new VerificationCode(); // 2.从VerificationCode类中获取BufferedImage对象; BufferedImage bufImage = vc.getImage(); // 3.同时获取验证码中的文本内容,并放到session域中, 用于校验; String code_text = vc.getText(); request.getSession().setAttribute("code_text", code_text); // 4.将生成的图片输出到客户端浏览器 VerificationCode.output(bufImage, response.getOutputStream()); }// end method-doGet public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // do same as GET-method : doGet(request, response); }// end method-doPost }part_4:成功登陆后的提示界面1:success-page-1.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%>"> <title>My JSP 'success-page-1.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"> --> </head> <% String username = (String) session.getAttribute("username"); if (null == username) { //如果username为空值,说明不是通过正常渠道来的,转发到Login页面; request.setAttribute("fdbkMsg", "别想走后门进来,赶紧登录!"); request.getRequestDispatcher("/LOGIN-DEMO/Login.jsp").forward( request, response); } %> <body> <br> <%=username%>已经成功登陆。 <br> <font>您可以选择浏览:</font> <br> <a href="/ServletDemoProject/LOGIN-DEMO/success-page-2.jsp">点这儿有精彩.</a> <br> <a href="/ServletDemoProject/LOGIN-DEMO/success-page-2.jsp">点这儿更精彩.</a> <br /> <a href="/ServletDemoProject/LOGIN-DEMO/success-page-2.jsp">你敢点这儿吗.</a> <br /> </body> </html>part_5:成功登陆后的提示界面1:success-page-2.jsp
<%@ page language="java" import="java.util.Date" pageEncoding="UTF-8"%> <%@ page language="java" import="java.text.SimpleDateFormat"%> <% 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%>"> <title>My JSP 'success-page-2.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"> --> </head> <% String username = (String) session.getAttribute("username"); if (null == username) { request.setAttribute("fdbkMsg", "呵呵嗒,这里是你来的地方吗?快登陆!"); //转发到登录界面: request.getRequestDispatcher("/LOGIN-DEMO/Login.jsp").forward( request, response); } SimpleDateFormat sDateFormat = new SimpleDateFormat("a"); Date today = new Date(); String am_pm_str = sDateFormat.format(today); String am_pm_str_in_chinese = ""; if ("am".equalsIgnoreCase(am_pm_str)) { am_pm_str_in_chinese = "上午"; } else am_pm_str_in_chinese = "下午"; // set null; sDateFormat = null; today = null; am_pm_str = null; %> <body> <br /> <font><b><%=username%><%=am_pm_str_in_chinese%>好,能来到页面2真不简单.</b> </font> </body> </html>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
2024年11月24日
2024年11月24日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]