javaweb验证码实现(jsp)
2017-12-29 23:30
253 查看
首先放上javaweb的目录结构:
生成验证码的jsp文件【validate.jsp】
【lohinFrom】
显示结果如下,直接点击验证码可以刷新,验证码刷新的时候,要加一个参数,是为了保证正常刷新。
验证验证码的正确性【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>
生成验证码的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>
相关文章推荐
- Jsp用Image Tag Library实现验证码
- jsp表单提交验证码功能实现
- JSP实现验证码
- javaweb登录验证码的实现
- JSP案例_简单实现验证码
- java web验证码生成总结(包括servlet、jsp和struts2实现)
- JSP实现验证码
- jsp实现验证码实例
- jsp servlet 实现登陆验证码
- jsp使用servlet实现用户登录 及动态验证码
- servlet+jsp实现验证码登录
- jsp中验证码的实现,以及ajax实现的正确的验证,解决了session不同步的问题。(我这里已测试过,可以直接用)
- JSP验证码的实现
- 主题:jsp实现验证码两个例子
- jsp+ajax实现的局部刷新较验验证码(onblur事件触发较验)
- javaweb中的验证码实现
- JAVA学习提高之---- JSP验证码的实现代码
- jsp实现字母和数字验证码功能
- 实现jsp验证码的简单小例子
- [JAVAWEB实战篇]---Jsp生成页面验证码的方法