JSP校验码,包括数字和字母

code.jsp页面用于生成校验码图片,代码如下:

<%@ page language="java"   pageEncoding="UTF-8"%>
<%@ page contentType="image/jpeg" import="java.awt.*, 
java.awt.image.*,java.util.*,javax.imageio.*" %> 
<%! 
//create by smallnest 
//email: [email protected] 
//website:www.kuaff.com 

//生成随机颜色 
Color getRandColor(Random random,int fc,int bc) 
{ 
        if(fc>255) fc=255; 
        if(bc>255) bc=255; 
        int r=fc+random.nextInt(bc-fc); 
        int g=fc+random.nextInt(bc-fc); 
        int b=fc+random.nextInt(bc-fc); 
        return new Color(r,g,b); 
} 
%> 
<% 
// 设置图片的长宽 
int width=176, height=30; 
//设置备选字符,剔除一些不雅的字符 
String base = "abcdefghijklmnopqrstuvwxyz1234567890"; 
//备选字符的长度 
int length = base.length(); 

//创建内存图像 
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
// 获取图形上下文 
Graphics g = image.getGraphics(); 

//创建随机类的实例 
Random random = new Random(); 

// 设定图像背景色(因为是做背景,所以偏淡) 
g.setColor(getRandColor(random,200,250)); 
g.fillRect(0, 0, width, height); 

//备选字体 
String[] fontTypes = {"\u5b8b\u4f53","\u65b0\u5b8b\u4f53","\u9ed1\u4f53","\u6977\u4f53","\u96b6\u4e66"}; 
int fontTypesLength = fontTypes.length; 

//在图片背景上增加噪点 
g.setColor(getRandColor(random,160,200)); 
g.setFont(new Font("Times New Roman",Font.PLAIN,14)); 
for (int i=0;i<6;i++) 
{ 
    g.drawString("*********************************************",0,5*(i+2)); 
} 

//保存生成的字符串 
String sRand=""; 
for (int i=0;i<6;i++) 
{ 
    int start = random.nextInt(length); 
    String rand=base.substring(start,start+1); 
    sRand+=rand; 
     
    //设置字体的颜色 
    g.setColor(getRandColor(random,10,150)); 
    //设置字体 
    g.setFont(new Font(fontTypes[random.nextInt(fontTypesLength)],Font.BOLD,18 + random.nextInt(6))); 
    //将此字符画到图片上 
    g.drawString(rand,24*i+ 10 + random.nextInt(8),24); 
} 

//将认证码存入session 
session.setAttribute("rand",sRand); 
g.dispose(); 

//输出图象到页面 
ImageIO.write(image, "JPEG", response.getOutputStream()); 
out.clear();
out = pageContext.pushBody();
%>
 

 

login.jsp用于访问的页面,校验码图片来自于code.jsp,form表单提交到check.jsp,代码如下:

<%@ page contentType="text/html;charset=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> 
<title>认证码输入页面</title> 
<META HTTP-EQUIV="Pragma" CONTENT="no-cache"> 
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache"> 
<META HTTP-EQUIV="Expires" CONTENT="0"> 
</head> 
<body> 
<form method="post" action="check.jsp"> 
	<table> 
		<tr> 
			<td align=left>系统产生的认证码:</td> 
			<td><img border=0 src="<%=basePath%>code.jsp"></td> 
		</tr> 
		<tr> 
			<td align=left>输入上面的认证码:</td> 
			<td><input type=text name=rand  value=""></td> 
		</tr> 
		<tr> 
			<td colspan=2 align=center><input type=submit value="提交检测"></td> 
		</tr> 
	</table>
</form> 
</body> 
</html> 

 check.jsp用于检验所输入校验码是否正确,代码如下:

<%@ page language="java"  pageEncoding="UTF-8"%>
<html> 
<head> 
<title>认证码验证页面</title> 
<META HTTP-EQUIV="Pragma" CONTENT="no-cache"> 
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache"> 
<META HTTP-EQUIV="Expires" CONTENT="0"> 
</head> 

<body> 
<% 
String rand = (String)session.getAttribute("rand"); 
String input = request.getParameter("rand"); 
%> 
系统产生的认证码为: <%= rand %><br> 
您输入的认证码为: <%= input %><br> 
<br> 
<% 
if (rand.equals(input)) { 
%> 
<font color=green>输入相同,认证成功!</font> 
<% 
} else { 
%> 
<font color=red>输入不同,认证失败!</font> 
<% 
} 
%> 
</body> 
</html> 

  

你可能感兴趣的:(JSP校验码)