您的位置:首页 > 编程语言 > Java开发

javaweb验证码实现(jsp)

2017-12-29 23:30 253 查看
首先放上javaweb的目录结构:



生成验证码的jsp文件【validate.jsp】

<%@ page
language="java"
import ="java.awt.*"
import ="java.awt.image.BufferedImage"
import ="java.util.*"
import ="javax.imageio.ImageIO"
pageEncoding="gb2312"%>
<%
response.setHeader("Cache-Control", "no-cache");
int width=60,height=20;
BufferedImage bufferedImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics graphics=bufferedImage.getGraphics();
graphics.setColor(new Color(200,200,200));
graphics.fillRect(0, 0, width, height);
Random random=new Random();
int randnum=random.nextInt(8999)+1000;
String ranString=String.valueOf(randnum);
session.setAttribute("randStr", ranString);
graphics.setColor(Color.BLACK);
graphics.setFont(new Font("",Font.PLAIN,20));
graphics.drawString(ranString, 10, 17);
for(int i=0;i<100;i++){
int x=random.nextInt(width);
int y=random.nextInt(height);
graphics.drawOval(x, y, 1, 1);
}
ImageIO.write(bufferedImage, "JPEG", response.getOutputStream());
out.clear();
out=pageContext.pushBody();
%>
生成之后直接放在session里面,也是为了后面提交之后的验证(这篇暂时不写验证)其中值得一提的是,这个不能直接访问,要是直接访问就是一堆乱码在浏览器上上显示出来,所以我们要用<a>标签来把它显示出来,显示页面如下:

【lohinFrom】

<%@ page language="java"
pageEncoding="gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">
<title>login</title>
<link href="css/login.css" type="text/css" rel="stylesheet">
</head>
<body>
<script type="text/javascript">
function refresh(){
loginform.code.src="validate.jsp?id="+Math.random();
}
</script>
<div class="login">
<div class="message">欢迎登录</div>
<hr class="hr15">
<div id="darkbannerwrap"></div>

<form name="loginform" method="post" action="judgevalidate.jsp">
<input name="action" value="login" type="hidden" >
<input class="username" name="username" placeholder="用户名" required="" type="text">
<input class="password" name="password" placeholder="密码" required="" type="password">
<input class="check_code" name="check_code" placeholder="验证码" required="" type="text"
style="width: 30%;">
<img class="img_code" src="validate.jsp" name="code" onclick="refresh()"/>
<div class="box">
<input class="loginin"type="submit" value="登陆">
</div>
</form>
</div>
</body>
</html>
其中使用到【login..css】文件

.login{
padding:20px;
margin: 0 auto;
width:200px;
height:300px;
border:1px solid #F00;
}
.message{
magin:0 auto;
text-align:center;
}
.loginin{
margin:0 auto;
width:80%;
display:block;
}
.img_code{
margin-left:30px;
margin-top:20px;
}
.box{
margin-top:30px;
}
.username{
width:100%;
margin-top:15px;
}
.password{
width:100%;
margin-top:15px;
}
.check_code{
margin-top:15px;
}

显示结果如下,直接点击验证码可以刷新,验证码刷新的时候,要加一个参数,是为了保证正常刷新。



验证验证码的正确性【judgevalidate.jsp】

<%@ page language="java"
import="java.io.PrintWriter"
pageEncoding="gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html">
<title>Insert title here</title>
</head>
<body>
<%
HttpSession httpSession=request.getSession();
String ranString=(String)httpSession.getAttribute("randStr");
String code=request.getParameter("check_code");
response.setCharacterEncoding("gb2312");
PrintWriter printWriter=response.getWriter();
if(code.equals(ranString)){
printWriter.write("验证码正确");
}else{
printWriter.write("验证码不正确");
}
%>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javaweb jsp 验证码 java