1、jsp页面
<form action="RegServlet" method="post">
用户名: <input type="text" name="userName" /><br/>
密 码: <input type="password" name="pwd" /><br/>
验证码:<input type="text" name="yzm" /><img id="yzm" title="点击刷新" src="VeryfServlet"/><br/>
<input type="submit" value="注册"/> <br/>
</form>
<script type="text/javascript" src="js/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function(){
$("#yzm").click(function(){
var sjs=Math.random();
$(this).attr("src","YzmServlet?r="+sjs);
});
});
</script>
2、验证码VeryfServlet
Color getRandColor(int fc, int bc) {
Random random = new Random();
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);
}
/**
* 生成数字验证码
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
void GenerateNumberVerify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
HttpSession session = request.getSession();
ServletOutputStream out = response.getOutputStream();
int width = 60, height = 30;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
String sRand = "";
for (int i = 0; i < 4; i++) {
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(rand, 13 * i + 6, 20);
}
session.setAttribute("code", sRand);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
out.flush();
out.close();
}
/**
* 生成混合验证码(英文,中文,数字)
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
void GenerateWordVerify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
HttpSession session = request.getSession();
ServletOutputStream out = response.getOutputStream();
int width = 70, height = 30;
String base ="你天爱风我没是在说美哈国日少飞牛四上三古下手右吃机浪水五花月二色好";
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( 200, 250));
g.fillRect(0, 0, width, height);
String[] fontTypes = {
"u5b8bu4f53", "u65b0u5b8bu4f53", "u9ed1u4f53", "u6977u4f53", "u96b6u4e66"};
int fontTypesLength = fontTypes.length;
g.setColor(getRandColor(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));
}
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 100; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
String sRand = "";
for (int i = 0; i < 4; i++) {
int start = random.nextInt(length);
String rand = base.substring(start, start + 1);
sRand += rand;
g.setColor(getRandColor(10, 150));
g.setFont(new Font(fontTypes[random.nextInt(fontTypesLength)], Font.BOLD, 14 + random.nextInt(6)));
g.drawString(rand, 15 * i + 0 + random.nextInt(6), 20);
}
session.setAttribute("code", sRand);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
out.flush();
out.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
GenerateWordVerify(request, response);
}
3、提交RegServlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String yzm=request.getParameter("yzm");
String code=(String)request.getSession().getAttribute("code");
if(yzm.equalsIgnoreCase(code)){
System.out.println("验证码正确,可以实现注册功能了!");
}else{
System.out.println("验证码输入有错!");
}
response.sendRedirect("reg.jsp");
}