本文实例为大家分享了jsp实现简单图片验证码的具体代码,供大家参考,具体内容如下
(1)在登陆页面加验证码的功能,起到一定的安全性。在输入正确的验证码,用户名和密码的情况下,才可以实现登录。
(2)实现查询数据库的功能。在登陆后的页面中,显示用户名和密码,并且设置有一个超链接,实现查询数据库的功能。
(3)代码核心是:随机生成验证码,并且显示在页面上。同时要和输入框中的输入验证码进行校验。
(4)主页面使用img标签的src属性引入验证页面的jsp文件。
(5)验证码的实现页面使用bufferedimage类的方法产生图片。
(6)使用graphics类的各种方法实现验证码的构成。
(1)登录页面:index.jsp文件。
<%@ page language="java" contenttype="text/html; chart=utf-8" pageencoding="utf-8"%><!doctype html><html><head><meta chart="utf-8"><title>登录页面</title></head><body><form action="loginrvlet" method="post"> 用户名:<input name="urname" type="text" value=""/><br/><br/> 密码:<input name="password" type="password" value=""/><br/><br/> 验证码: <input type="text" name="checkcode" height="20px " value=""/> <img src="codervlet"/><span>${error_code}</span><br/> <input type="submit" value="提交"></form></body></html>
(2)登录后的页面:ur.jsp文件。
<%@ page language="java" contenttype="text/html; chart=utf-8" pageencoding="utf-8"%><%@ page import = "com.entity.author"%><!doctype html><html><head><meta chart="utf-8"><title>显示登录用户的用户名和密码页面</title></head><body><% //内置对象 request.tcharacterencoding("utf-8"); //获取交互层放入ssion中的obj author obj = (author)ssion.getattribute("authorinfo"); if(obj != null){ out.print("<p>用户名:"+obj.getname()+"</p>"); out.print("<p>密码:"+obj.getid()+"</p>"); } el{ respon.ndredirect("index.jsp"); }%><br/><a href="authorrvlet">用户信息查询 </a></body></html>
(3)实现数据查询页面:ueslist.jsp文件。
<%@ page language="java" contenttype="text/html; chart=utf-8" pageencoding="utf-8"%><%@ taglib prefix="c" uri="/d/file/titlepic/" %><!doctype html><html><head><meta chart="utf-8"><title>查询信息显示页面</title></head><body><table border="1"> <tr> <td>编号</td> <td>名称</td> <td>价格</td> <td>数量</td> <td>日期</td> <td>风格</td> </tr> <c:foreach items="${authorlist}" var="author"> <tr> <td>${author.id}</td> <td>${author.name }</td> <td>${author.price }</td> <td>${author.num }</td> <td>${auth双高计划or.dates}</td> <td>${author.style}</td> </tr> </c:foreach></table></body></html>
(4)定义一个author类,用于接收数据库中的元素。
package com.entity;//用于获取数据库中的元素对象public class author { private int id; private string name; private int price ; private int num; private string dates; private string style; public int getid() { return id; } public void tid(int id) { this.id = id; } public string getname() { return name; } public void 奔腾电器广告tname(string name) { this.name = name; } public int getprice() { return price; } public void tprice(int price) { this.price = price; } public int getnum() { return num; } public void tnum(int num) { this.num = num; } public string getdates() { return dates; } public void tdates(string dates) { this.dates = dates; } public string getstyle() { return style; } public void tstyle(string style) { this.style = style; }}
(5)登录页面的交互层:loginrvlet.java文件。用于登录检验和验证码匹配。
//交互层(客户端和服务器的交互)package com.rvlet;import java.io.ioexception;import java.sql.sqlexception;import javax.rvlet.rvletexception;import javax.rvlet.annotation.webrvlet;import javax.rvlet.http.httprvlet;import javax.rvlet.http.httprvletrequest;import javax.rvlet.http.httprvletrespon;import javax.rvlet.http.httpssion;import com.dao.authordao;import com.entity.author;/*** rvlet implementation class loginrvlet*/@webrvlet("/loginrvlet")public class loginrvlet extends httprvlet { private static final long rialversionuid = 1l; /** * @e httprvlet#httprvlet() */ public loginrvlet() { super(); // todo auto-generated constructor stub } /** * @e httprvlet#doget(httprvletrequest request, httprvletrespon respon) */ protected void doget(httprvletrequest request, httprvletrespon respon) throws rvletexception, ioexception { // todo auto-generated method stub //内置对象request,respon request.tcharacterencoding("utf-8"); httpssion ssion = request.getssion(); //获取用户输入验证码 string checkcode = request.getparameter("checkcode"); //获取ssion中的验证码,也就是codervlet中生成的四个字符 string ssioncode = (string)ssion.getattribute("scode"); //验证码正确 if(checkcode.equals(ssioncode)) { //获取表单数据 string urname = request.getparameter("urname"); int password = integer.valueof(request.getparameter("password")); //判断用户信息是否正确,查询数据库获取用户信息 authordao ad = new authordao(); author obj = ad.check(urname, password); //判断 if(obj != null) { //重新放入用户信息 // httpssion ssion = request.getssion(); ssi搞笑祝福语on.tattribute("authorinfo", obj); //设置ssion的有效期为10秒 ssion.tmaxinactiveinterval(10); //页面转发 respon.ndredirect("ur.jsp"); } el { //页面重定向到登录页面 respon.ndredirect("index.jsp"); } } el { //验证码不正确 request.tattribute("error_code", "验证码不匹配"); request.二季度思想汇报getrequestdispatcher("index.jsp").forward(request, respon); } } /** * @e httprvlet#dopost(httprvletrequest request, httprvletrespon respon) */ protected void dopost(httprvletrequest request, httprvletrespon respon) throws rvletexception, ioexception { // todo auto-generated method stub doget(request, respon); }}
(6)数据库查询的交互层:authorrvlet.java文件。
package com.rvlet;import java.io.ioexception;import java.util.list;import javax.rvlet.rvletexception;import javax.rvlet.annotation.webrvlet;import javax.rvlet.http.httprvlet;import javax.rvlet.http.httprvletrequest;import javax.rvlet.http.httprvletrespon;import com.dao.authordao;import com.entity.author;/*** rvlet implementation class authorrvlet*/@webrvlet("/authorrvlet")public class authorrvlet extends httprvlet { private static final long rialversionuid = 1l; /** * @e httprvlet#httprvlet() */ public authorrvlet() { super(); // todo auto-generated constructor stub } /** * @e httprvlet#doget(httprvletrequest request, httprvletrespon respon) */ protected void doget(httprvletrequest request, httprvletrespon respon) throws rvletexception, ioexception { // todo auto-generated method stub //设置编码方式 request.tcharacterencoding("utf-8"); //查询用户列表 authordao ad = new authordao(); //将dao层中的结果放入list中 list<author三角恒等变换> list = ad.queryauthorlist(); request.tattribute("authorlist", list); //请求转发的方式将查询结果放入request中,再将超链接直接访问authorrvlet就将信息显示出来了。 request.getrequestdispatcher("ulist.jsp").forward(request, respon); } /** * @e httprvlet#dopost(httprvletrequest request, httprvletrespon respon) */ protected void dopost(httprvletrequest request, httprvletrespon respon) throws rvletexception, ioexception { // todo auto-generated method stub doget(request, respon); }}
(7)定义一个authordao类实现查询数据库和检验登录的用户名和密码。
//用于检验登录页面所填入信息是否正确package com.dao;import java.sql.connection;import java.sql.preparedstatement;import java.sql.resultt;import java.sql.sqlexception;import java.util.arraylist;import java.util.list;import com.entity.author;public class authordao { public author check(string urname ,int password) { author obj = null ; try { dbconnection db = new dbconnection(); //获取数据库连接 connection conn = db.getconn(); //设置要执行的数据库语句 string sql = "lect *from furnitures where name = ? and id = ?"; preparedstatement ps = conn.preparestatement(sql); //设置用户名和密码放入sql语句 ps.tstring(1, urname); ps.tint(2, password); //执行sql查询语句 , 并将执行结果放入结果集中 resultt rs = ps.executequery(); //用户名和密码都正确 if(rs.next()) { //新创建一个obj 将查询结果放入 obj = new author(); obj.tid(rs.getint(1)); obj.tname(rs.getstring(2)); obj.tprice(rs.getint(3)); obj.tnum(rs.getint(4)); obj.tdates(rs.getstring(5)); obj.tstyle(rs.getstring(6)); } } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } return obj; } public list<author> queryauthorlist(){ author obj = null; //定义一个list集合,用于存放查询结果 list<author> list = new arraylist<author>() ; try { dbconnection db = new dbconnection(); //获取数据库连接 connection conn = db.getconn(); //设置数据库要查询的语句 string sql = "lect *from furnitures "; preparedstatement ps = conn.preparestatement(sql); //执行数据库查询语句,并将查询结果放入结果集 resultt rs = ps.executequery(); //利用循环将obj放入list集合中 while(rs.next()) { obj = new author(); obj.tid(rs.getint(1)); obj.tname(rs.getnstring(2)); obj.tprice(rs.getint(3)); obj.tnum(rs.getint(4)); obj.tdates(rs.getstring(5)); obj.tstyle(rs.getstring(6)); //将obj加入到list list.add(obj); } } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } return list; } }
(8)定义一个验证码生成codervlet类,用于生成验证码。
package com.rvlet;import java.awt.color;import java.awt.font;import java.awt.graphics;import java.awt.image.bufferedimage;import java.io.ioexception;import java.util.random;import javax.imageio.imageio;import javax.rvlet.rvletexception;import javax.rvlet.rvletoutputstream;import javax.rvlet.annotation.webrvlet;import javax.rvlet.http.httprvlet;import javax.rvlet.http.httprvletrequest;import javax.rvlet.http.httprvletrespon;import javax.rvlet.http.httpssion;@webrvlet("/codervlet")public class codervlet extends httprvlet{ //定义验证码的源码 private static final string str ="abcdefghijklmnopqrstuvwxyaabcdefghijklmnopqrstuvwxyz1234567890"; //定义随机数 private random random = new random(); //随机生成四个字符 public string getstr() { string s = ""; int len = str.length(); for(int i=0;i<4;i++) { s+=str.charat(random.nextint(len)); } return s; } //随机颜色 public color getcolor() { int red = random.nextint(256); int green = random.nextint(256); int blue = random.nextint(256); color color = new color(red,green,blue); return color; } @override protected void doget(httprvletrequest req, httprvletrespon resp) throws rvletexception, ioexception { // todo auto-generated method stub //生成验证码图片 //画板 bufferedimage image = new bufferedimage(70,20,bufferedimage.type_int_rgb ); //画笔 graphics pen = image.getgraphics(); //矩形 pen.fillrect(0, 0, 70, 20); //字体 pen.tfont(new font("微软雅黑",font.bold,20)); //获取4个字符 string code = getstr(); //绘制图片 for(int i=0;i<code.length();i++) { pen.tcolor(getcolor()); pen.drawstring(string.valueof(code.charat(i)), i*15+5, 20);; } //respon对象绘制图片到页面,rvle输出流进行图片的输出 rvletoutputstream sos = resp.getoutputstream(); imageio.write(image, "png", sos); sos.flush(); sos.clo(); //验证码放入ssion httpssion ssion = req.getssion(); ssion.tattribute("scode", code); } @override protected void dopost(httprvletrequest req, httprvletrespon resp) throws rvletexception, ioexception { // todo auto-generated method stub dopost(req, resp); }}
(9)创建dbconnectoin.java类用户获取数据库连接。(我用的是mysql)
//获取数据库连接package com.dao;import java.sql.connection;import java.sql.drivermanager;import java.sql.sqlexception;public class dbconnection { private static string urname="填入自己的数据库名"; private static string password="填入自己的数据库密码"; private static string driver = "com.mysql.jdbc.driver"; private static string url="jdbc:mysql://localhost:3306/已经创建数据库名"; private connection conn; static { try { //加载驱动,捕获异常 class.forname(driver); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } } public dbconnection () throws sqlexception { //连接数据库 conn = drivermanager.getconnection(url,urname,password); } //用于获取conn public connection getconn() { return conn; } public void tconn(connection conn) { this.conn = conn; } }
(1)登录页面
(2)数据查询页面
(3)查询结果显示页面
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 23:33:39,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/381d27d2b7e4d828f53411b9976ea51e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:jsp实现简单图片验证码功能.doc
本文 PDF 下载地址:jsp实现简单图片验证码功能.pdf
留言与评论(共有 0 条评论) |