动手实现随机验证码
来源:互联网 发布:淘宝怎么换海报 编辑:程序博客网 时间:2024/05/19 14:38
动手实现随机验证码
首先,来写一个画验证码图片的Java类:
package com.XiongYi.validationCode;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ValidationCode extends HttpServlet...{
public static String getValidationCodes(OutputStream os)...{
char codeTable[]=...{
'a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','P','Q','R','S','T','U','V','W','X','Y','Z',
'1','2','3','4','5','6','7','8','9'
}; //判断时大小写不区分,另外,没有使用'O'和'0',也是怕混淆的麻烦
BufferedImage image = new BufferedImage(60, 20,
BufferedImage.TYPE_INT_RGB);
//生成60×20的RGB图片
Graphics graphic = image.getGraphics();
//背景色
graphic.setColor(new Color(0xDCDCDC));
graphic.fillRect(0, 0, 60, 20);
char codes[] = new char[4];
//随机产生验证码
for(int i=0; i<=3; i++) ...{
codes[i] = codeTable[(int)(codeTable.length*Math.random())];
}
//把字画上去
graphic.setColor(Color.BLACK);
graphic.setFont(new Font("Arial", Font.BOLD, 16));
for(int i=0; i<=3; i++)...{
graphic.drawChars(codes, i, 1, 2+15*i, 15+i);
//把codes[i]画到坐标为[10+5*i,15+i]的地方去
}
//15个干扰点
Random rand = new Random();
for (int i=0;i<15;i++)
graphic.drawOval(rand.nextInt(60),rand.nextInt(20),1,1);
//释放此图形的上下文并释放它所使用的所有系统资源
graphic.dispose();
try ...{
ImageIO.write(image, "JPEG", os);
} catch (IOException e) ...{
e.printStackTrace();
}
return new String(codes,0,codes.length);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
request.getSession().setAttribute("validationCodes", getValidationCodes(response.getOutputStream()));
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ValidationCode extends HttpServlet...{
public static String getValidationCodes(OutputStream os)...{
char codeTable[]=...{
'a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','P','Q','R','S','T','U','V','W','X','Y','Z',
'1','2','3','4','5','6','7','8','9'
}; //判断时大小写不区分,另外,没有使用'O'和'0',也是怕混淆的麻烦
BufferedImage image = new BufferedImage(60, 20,
BufferedImage.TYPE_INT_RGB);
//生成60×20的RGB图片
Graphics graphic = image.getGraphics();
//背景色
graphic.setColor(new Color(0xDCDCDC));
graphic.fillRect(0, 0, 60, 20);
char codes[] = new char[4];
//随机产生验证码
for(int i=0; i<=3; i++) ...{
codes[i] = codeTable[(int)(codeTable.length*Math.random())];
}
//把字画上去
graphic.setColor(Color.BLACK);
graphic.setFont(new Font("Arial", Font.BOLD, 16));
for(int i=0; i<=3; i++)...{
graphic.drawChars(codes, i, 1, 2+15*i, 15+i);
//把codes[i]画到坐标为[10+5*i,15+i]的地方去
}
//15个干扰点
Random rand = new Random();
for (int i=0;i<15;i++)
graphic.drawOval(rand.nextInt(60),rand.nextInt(20),1,1);
//释放此图形的上下文并释放它所使用的所有系统资源
graphic.dispose();
try ...{
ImageIO.write(image, "JPEG", os);
} catch (IOException e) ...{
e.printStackTrace();
}
return new String(codes,0,codes.length);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
request.getSession().setAttribute("validationCodes", getValidationCodes(response.getOutputStream()));
}
}
然后,写一个Servlet将生成的图片写入页面:
package com.XiongYi.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.XiongYi.validationCode.ValidationCode;
public class ValidationServlet extends HttpServlet ...{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
String str = ValidationCode.getValidationCodes(response.getOutputStream());
request.getSession().setAttribute("validationCodes", str);
}
}
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.XiongYi.validationCode.ValidationCode;
public class ValidationServlet extends HttpServlet ...{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
String str = ValidationCode.getValidationCodes(response.getOutputStream());
request.getSession().setAttribute("validationCodes", str);
}
}
接着,在login.jsp中加入如下表单:
<%...
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<form action="<%=basePath+"servlet/LoginServlet"%>">
<img src="<%=basePath+"servlet/ValidationServlet"%>"/>
<br>
<input type="text" name="codes"/>
<input type="submit"/>
</form>
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<form action="<%=basePath+"servlet/LoginServlet"%>">
<img src="<%=basePath+"servlet/ValidationServlet"%>"/>
<br>
<input type="text" name="codes"/>
<input type="submit"/>
</form>
再写进行后台判断的Servlet:
package com.XiongYi.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet ...{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
if(request.getSession().getAttribute("validationCodes").toString().toLowerCase()
.equals(request.getParameter("codes").toString().toLowerCase()))
response.getOutputStream().println("Right!");
else
response.getOutputStream().println("Wrong!");
}
}
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet ...{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
if(request.getSession().getAttribute("validationCodes").toString().toLowerCase()
.equals(request.getParameter("codes").toString().toLowerCase()))
response.getOutputStream().println("Right!");
else
response.getOutputStream().println("Wrong!");
}
}
怎么样,很容易吧,那就快尝试一下吧!o(∩_∩)o...
- 动手实现随机验证码
- Servlet实现随机验证码
- 如何实现随机验证码
- 随机图片验证码实现
- Ext随机验证码实现
- js 实现随机验证码
- 随机验证码实现案例
- Java 实现随机验证码
- 画布实现随机验证码
- 用Java实现随机验证码功能
- Java实现随机验证码功能实例
- Java实现随机验证码功能实例
- Java实现随机验证码功能实例
- Java实现随机验证码功能实例
- Java实现随机验证码功能实例
- 用PHP实现随机验证码功能
- Java实现随机验证码功能实例
- Java实现随机验证码功能
- div panel frame 高度 宽度
- ASP.NET 2.0中GridView无限层复杂表头的实现
- 注册VB.NET dll
- Google薪水几何?
- ASP.NET 2.0中合并 GridView 的表头单元格
- 动手实现随机验证码
- TreeView绑定数据库
- Creating Office 2000的.NET解决方案
- 利用GridView显示主细表并一次编辑明细表所有数据
- 关于软件的架构设计
- 追本溯源 - 勿忘质量之本
- 使用Zend_Feed+Cron打造Feed Reader
- 关于BIOS的那些事----不要老整三岁的脑筋急转弯,咱们来整点五岁的(上)
- 开元系统2.0框架平台