Cookie与Session之(登录验证码与记住我 示例)
2020-02-02 13:59
281 查看
1 package com.ajax.entity; 2 //需要bootstrap, 3 import java.io.Serializable; 4 import java.util.Date; 5 6 public class Student implements Serializable { 7 private Integer stuId;//int(5) NOT NULL学生id,主键 8 private String stuName;//varchar(30) NOT NULL学生姓名 9 private String pwd;//varchar(255) NULL密码 10 private Integer stuAge;//int(11) NOT NULL学生年龄 11 private String stuAddr;//varchar(255) NULL学生的住址 12 private String likes;//varchar(255) NULL学生爱好 13 private String stuSex;//char(3) NOT NULL学生性别 14 private String degree;//varchar(50) NULL学生学历 15 private Integer rid;//int(4) NOT NULL关联的角色id 16 private Date createdate;//date NULL创建日期 17 private Date lastmodigydate;//date NULL最后一次修改日期 18 19 @Override 20 public String toString() { 21 return "Student{" + 22 "stuId=" + stuId + 23 ", stuName='" + stuName + '\'' + 24 ", pwd='" + pwd + '\'' + 25 ", stuAge=" + stuAge + 26 ", stuAddr='" + stuAddr + '\'' + 27 ", likes='" + likes + '\'' + 28 ", stuSex='" + stuSex + '\'' + 29 ", degree='" + degree + '\'' + 30 ", rid=" + rid + 31 ", createdate=" + createdate + 32 ", lastmodigydate=" + lastmodigydate + 33 '}'; 34 } 35 36 public Integer getStuId() { 37 return stuId; 38 } 39 40 public void setStuId(Integer stuId) { 41 this.stuId = stuId; 42 } 43 44 public String getStuName() { 45 return stuName; 46 } 47 48 public void setStuName(String stuName) { 49 this.stuName = stuName; 50 } 51 52 public String getPwd() { 53 return pwd; 54 } 55 56 public void setPwd(String pwd) { 57 this.pwd = pwd; 58 } 59 60 public Integer getStuAge() { 61 return stuAge; 62 } 63 64 public void setStuAge(Integer stuAge) { 65 this.stuAge = stuAge; 66 } 67 68 public String getStuAddr() { 69 return stuAddr; 70 } 71 72 public void setStuAddr(String stuAddr) { 73 this.stuAddr = stuAddr; 74 } 75 76 public String getLikes() { 77 return likes; 78 } 79 80 public void setLikes(String likes) { 81 this.likes = likes; 82 } 83 84 public String getStuSex() { 85 return stuSex; 86 } 87 88 public void setStuSex(String stuSex) { 89 this.stuSex = stuSex; 90 } 91 92 public String getDegree() { 93 return degree; 94 } 95 96 public void setDegree(String degree) { 97 this.degree = degree; 98 } 99 100 public Integer getRid() { 101 return rid; 102 } 103 104 public void setRid(Integer rid) { 105 this.rid = rid; 106 } 107 108 public Date getCreatedate() { 109 return createdate; 110 } 111 112 public void setCreatedate(Date createdate) { 113 this.createdate = createdate; 114 } 115 116 public Date getLastmodigydate() { 117 return lastmodigydate; 118 } 119 120 public void setLastmodigydate(Date lastmodigydate) { 121 this.lastmodigydate = lastmodigydate; 122 } 123 124 public Student() { 125 } 126 127 public Student(Integer stuId, String stuName, String pwd, Integer stuAge, String stuAddr, String likes, String stuSex, String degree, Integer rid, Date createdate, Date lastmodigydate) { 128 this.stuId = stuId; 129 this.stuName = stuName; 130 this.pwd = pwd; 131 this.stuAge = stuAge; 132 this.stuAddr = stuAddr; 133 this.likes = likes; 134 this.stuSex = stuSex; 135 this.degree = degree; 136 this.rid = rid; 137 this.createdate = createdate; 138 this.lastmodigydate = lastmodigydate; 139 } 140 }
1 package com.ajax.dao; 2 3 import com.ajax.entity.Student; 4 import com.ajax.util.DataUtil; 5 import org.apache.commons.dbutils.QueryRunner; 6 import org.apache.commons.dbutils.handlers.BeanHandler; 7 8 import java.sql.SQLException; 9 10 public class StudentDao { 11 12 private QueryRunner runner = new QueryRunner(DataUtil.getDataSource()); 13 14 public Student findStudentByName(String name){ 15 try { 16 return runner.query("select * from tblstudent where stuName = ?",new BeanHandler<>(Student.class),name); 17 } catch (SQLException e) { 18 e.printStackTrace(); 19 throw new RuntimeException("findStudentByName出现异常"); 20 } 21 } 22 23 public int doAddStudent(String name,String pwd){ 24 try { 25 return runner.update("insert into tblstudent(stuName,pwd)values(?,?)",name,pwd); 26 } catch (SQLException e) { 27 e.printStackTrace(); 28 throw new RuntimeException("doAddStudent出现异常"); 29 } 30 } 31 32 }
1 package com.ajax.servlet; 2 3 import com.ajax.dao.StudentDao; 4 import com.ajax.entity.Student; 5 6 import javax.imageio.ImageIO; 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 import javax.servlet.http.HttpSession; 13 import java.awt.*; 14 import java.awt.image.BufferedImage; 15 import java.io.IOException; 16 import java.io.PrintWriter; 17 import java.util.Random; 18 19 @WebServlet("/check") 20 public class CheckServlet extends HttpServlet { 21 22 private StudentDao sd = new StudentDao(); 23 24 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 25 doGet(request,response); 26 } 27 28 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 29 String check = request.getParameter("check"); 30 if ("username".equals(check)){ 31 checkName(request, response); 32 }else if ("code".equals(check)){//生成验证码图片 33 checkCode(request,response); 34 }else if ("checkcode".equals(check)){// /check?check=checkcode&codename 35 checkcodevalue(request,response); 36 }else if ("add".equals(check)){ 37 addStudent(request,response); 38 } 39 } 40 41 private void addStudent(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 42 String name = request.getParameter("vname"); 43 String pwd = request.getParameter("vpwd"); 44 // PrintWriter out = response.getWriter(); 45 if (name != null && pwd != null){ 46 int i = sd.doAddStudent(name, pwd); 47 // out.print(i); 48 } 49 // out.close(); 50 // response.sendRedirect("/jsp/Login.jsp"); 51 request.getRequestDispatcher("/jsp/Login.jsp").forward(request,response); 52 } 53 54 private void checkcodevalue(HttpServletRequest request, HttpServletResponse response) throws IOException { 55 String codevalue = request.getParameter("codename"); 56 HttpSession session = request.getSession(); 57 //从session中拿到验证码图片, 58 String codeimg = (String) session.getAttribute("code"); 59 PrintWriter out = response.getWriter(); 60 boolean iscode = false; 61 if (codevalue.equalsIgnoreCase(codeimg)){ 62 iscode = true; 63 } 64 out.print(iscode); 65 out.close(); 66 } 67 68 /** 69 * 生成验证码图片 70 * @param request 71 * @param response 72 * @throws IOException 73 */ 74 private void checkCode(HttpServletRequest request, HttpServletResponse response) throws IOException { 75 //get请求; 76 // 创建画布 77 int width = 120; 78 int height = 40; 79 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 80 // 获得画笔 81 Graphics g = bufferedImage.getGraphics(); 82 // 填充背景颜色 83 g.setColor(Color.white); 84 g.fillRect(0, 0, width, height); 85 // 绘制边框 86 g.setColor(Color.red); 87 g.drawRect(0, 0, width - 1, height - 1); 88 // 生成随机字符 89 // 准备数据 90 String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; 91 // 准备随机对象 92 Random r = new Random(); 93 // 声明一个变量 保存验证码 94 String code = ""; 95 // 书写4个随机字符 96 for (int i = 0; i < 4; i++) { 97 // 设置字体 98 g.setFont(new Font("宋体", Font.BOLD, 28)); 99 // 设置随机颜色 100 g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255))); 101 String str = data.charAt(r.nextInt(data.length())) + ""; 102 g.drawString(str, 10 + i * 28, 30); 103 // 将新的字符 保存到验证码中 104 code = code + str; 105 } 106 // 绘制干扰线 107 for (int i = 0; i < 6; i++) { 108 // 设置随机颜色 109 g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255))); 110 g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height)); 111 } 112 // 将验证码 打印到控制台 113 // 将验证码放到session中 114 request.getSession().setAttribute("code", code); 115 // 将画布显示在浏览器中 116 ImageIO.write(bufferedImage, "jpg", response.getOutputStream()); 117 } 118 119 /** 120 * 检查用户名 121 * @param request 122 * @param response 123 * @throws IOException 124 */ 125 private void checkName(HttpServletRequest request, HttpServletResponse response) throws IOException { 126 Student student = sd.findStudentByName(request.getParameter("name")); 127 //通过响应对象,输出信息 128 PrintWriter out = response.getWriter(); 129 String msg = "0";//存在不可以注册 130 if (student==null){ 131 msg = "1"; 132 } 133 out.print(msg); 134 out.close(); 135 } 136 137 138 139 }
1 package com.ajax.util; 2 3 import com.mchange.v2.c3p0.ComboPooledDataSource; 4 5 import javax.sql.DataSource; 6 import java.sql.Connection; 7 import java.sql.SQLException; 8 9 public class DataUtil { 10 private static ComboPooledDataSource dataSource = new ComboPooledDataSource("mysql"); 11 public static Connection getConnection(){ 12 try { 13 return dataSource.getConnection(); 14 } catch (SQLException e) { 15 e.printStackTrace(); 16 throw new RuntimeException("获取连接对象Connection异常。。。"); 17 } 18 } 19 public static DataSource getDataSource(){ 20 return dataSource; 21 } 22 23 public static void closeConnection(Connection con){ 24 if (con!=null){ 25 try { 26 con.close(); 27 } catch (SQLException e) { 28 e.printStackTrace(); 29 } 30 } 31 } 32 }
1 <%-- 2 Created by IntelliJ IDEA. 3 User: Administrator 4 Date: 2019/8/30 5 Time: 12:25 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <html> 10 <head> 11 <title>注册(验证码)页面</title> 12 <link rel="stylesheet" href="../bootstrap/css/bootstrap.min.css"> 13 <script src="../bootstrap/js/jquery.min.js"></script> 14 <script src="../bootstrap/js/bootstrap.min.js"></script> 15 </head> 16 <body> 17 <div class="container"> 18 <div class="panel panel-primary"> 19 <div class="panel panel-heading"> 20 <h1>注册页面</h1> 21 </div> 22 <div class="panel-body"> 23 <form class="form-horizontal" role="form" action="/check?check=add" method="post" 24 οnsubmit="return checkForm()"> 25 <%--οnsubmit="return checkForm()">--%> 26 <div class="form-group"> 27 <label for="uname" class="col-sm-3 control-label">姓名</label> 28 <div class="col-sm-4"> 29 <input type="text" class="form-control" id="uname" name="uname" placeholder="请输入用户名。。。"> 30 </div> 31 <%--<div class="span col-md-5"></div>--%> 32 <div class="span col-md-5" id="s1"></div> 33 </div> 34 <div class="form-group"> 35 <label for="upwd" class="col-sm-3 control-label">密码</label> 36 <div class="col-sm-4"> 37 <input type="password" class="form-control" id="upwd" placeholder="请输入用户密码。。。"> 38 </div> 39 <%--<div class="span col-md-5"></div>--%> 40 <div class="span col-md-5" id="s2"></div> 41 </div> 42 <div class="form-group"> 43 <label for="code" class="col-sm-3 control-label">验证码</label> 44 <div class="col-sm-3"> 45 <input type="text" class="form-control" id="code" placeholder="请输入验证码。。。。"> 46 </div> 47 <%--验证码--%> 48 <div class="col-md-2"><img src="/check?check=code" style="cursor: pointer;"></div> 49 <div class="span"></div> 50 </div> 51 <div class="form-group"> 52 <div class="col-sm-offset-5 col-sm-6"> 53 <button type="submit" id="id_btn" class="btn btn-default">提交注册</button> 54 </div> 55 </div> 56 </form> 57 </div> 58 <div class="panel-footer text-center"> 59 李某人为您服务 60 </div> 61 </div> 62 </div> 63 </body> 64 <script> 65 $(function () { 66 //验证码图片点击事件 67 $("img").click(function () { 68 $(this).attr("src", "/check?check=code&time=" + (new Date())); 69 /*加上时间戳(单击时)才生效*/ 70 }); 71 72 //验证表单中的数据是否有效// 使用ajax进行验证 73 $("#uname").blur(function () {//失去焦点时触发ajax 74 var vname = $(this).val(); 75 if (vname == "" || vname == null || vname == undefined) { 76 $(".span:eq(0)").text("用户名不能为空"); 77 } else { 78 //参数为:url、参数、回调函数(返回的信息数据)、返回数据类型(txt、json、xml。。。) 79 $.post("/check?check=username", "name=" + vname, function (data) { 80 if (data == 1) { 81 $(".span:eq(0)").text(""); 82 83 var span3 = $(".span:eq(2)").text(); 84 if (span3 == "") { 85 $("#id_btn").attr("disabled", false); //禁用提交 按钮! 86 } else { 87 $("#id_btn").attr("disabled", true); //禁用提交 按钮! 88 } 89 } else { 90 $(".span:eq(0)").text("姓名重复"); 91 $("#id_btn").attr("disabled", true); //禁用提交 按钮! 92 } 93 // $(".span:eq(0)").text(data==1?"姓名OK":"姓名无效"); 94 }, "text"); 95 } 96 }); 97 98 //密码失去焦点 99 $("#upwd").blur(function () {//失去焦点时触发ajax 100 var vpwd = $(this).val(); 101 if (vpwd == "" || vpwd == null || vpwd == undefined) { 102 $(".span:eq(1)").text("密码不能为空"); 103 } else { 104 $(".span:eq(1)").text(""); 105 } 106 }); 107 108 //验证码失去光标事件,不能为空与进行匹配时间 109 $("#code").blur(function () { 110 var vcode = $(this).val(); 111 if (vcode == "" || vcode == null || vcode == undefined) { 112 $(".span:eq(2)").text("验证码不能为空"); 113 } else { 114 //进行验证码判断,使用ajax进行一步判断 115 //使用ajax的get方式,参数:url包含参数,回调函数(返回的数据)、返回的数据类型 116 $.get("/check?check=checkcode&codename=" + vcode, function (data) { 117 if (data == "false") { 118 $(".span:eq(2)").text("验证码错误"); 119 $("#id_btn").attr("disabled", true); //禁用提交 按钮! 120 } else { 121 $(".span:eq(2)").text(""); 122 var span1 = $(".span:eq(0)").text(); 123 if ((span1 == "")) { 124 $("#id_btn").attr("disabled", false); //禁用提交 按钮! 125 } else { 126 $("#id_btn").attr("disabled", true); //禁用提交 按钮! 127 } 128 } 129 // $(".span:eq(2)").text(data=="false"?"验证码错误":"OK"); 130 }, "text"); 131 } 132 }); 133 134 }); 135 136 /** 137 * 验证码不能为空 138 * */ 139 function cm() { 140 var vcode = $("#code").val(); 141 if (vcode == "" || vcode == null || vcode == undefined) { 142 $(".span:eq(2)").text("验证码不能为空"); 143 return false; 144 } else { 145 $(".span:eq(2)").text(""); 146 return true; 147 } 148 } 149 150 /* 151 * 密码不能为空 152 * */ 153 function bm() { 154 var vpwd = $("#upwd").val(); 155 if (vpwd == "" || vpwd == null || vpwd == undefined) { 156 $(".span:eq(1)").text("密码不能为空"); 157 return false; 158 } else { 159 $(".span:eq(1)").text(""); 160 return true; 161 } 162 } 163 164 /** 165 * 用户名不能为空 166 */ 167 function am() { 168 var vname = $("#uname").val(); 169 if (vname == "" || vname == null || vname == undefined) { 170 $(".span:eq(0)").text("用户名不能为空"); 171 return false; 172 } else { 173 $(".span:eq(0)").text(""); 174 return true; 175 } 176 } 177 178 function checkForm() { 179 var a = am(); 180 var b = bm(); 181 var c = cm(); 182 var r = a & b & c; 183 alert(r); 184 return r > 0; 185 } 186 </script> 187 </html>
转载于:https://www.cnblogs.com/in-the-game-of-thrones/p/11451213.html
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- cookie、session及实现记住密码,自动登录
- session、cookie与“记住我的登录状态”的功能的实现
- cookie、session及实现记住密码,自动登录
- Cookie、Session实现“记住登录状态”的原理
- cookie、session及实现记住密码,自动登录
- cookie、session及实现记住密码,自动登录
- session、cookie与“记住我的登录状态”的功能的实现
- session、cookie与“记住我的登录状态”的功能的实现
- Session、Cookie 记住登录状态的实现
- session、cookie与“记住我的登录状态”的功能的实现
- session、cookie与“记住我的登录状态”的功能的实现
- cookie、session及实现记住密码,自动登录
- WEB-11-JSP概述-1-会话/cookie/session/Ajax-记住密码/登录
- 登录时记住密码——Filter、Cookie和Session的简单应用
- JavaWeb学习记录总结(二十九)--Servlet\Session\Cookie\Filter实现自动登录和记住密码
- Python Selenium Cookie 绕过验证码实现登录示例代码
- session、cookie与“记住我的登录状态”的功能的实现
- 【JavaWeb-7】Cookie记住用户名、历史浏览记录与Session的自动系列化、购物车案例、验证码使用
- javaWeb的session和cookie实现记住密码自动登录功能简单实例
- (转)session、cookie与“记住我的登录状态”的功能的实现