一个Servlet验证码
转来自http://jspwind.com.cn
.首先在web.xml的配置:
<servlet>
<servlet-name> CodeServlet </servlet-name> <!-- 设定servlet名 -->
<servlet-class> com.bbs.logic.CodeGenServlet </servlet-class> <!-- 指定servlet的class路径 -->
<!-- <load-on-startup> 5 </load-on-startup> -->
</servlet>
<servlet-mapping>
<servlet-name> CodeServlet </servlet-name> <!-- 指定servlet名 -->
<url-pattern> /check/UrlTest.jsp </url-pattern> <!-- 指定servlet路径 放在WebRoot/check目录下-->
</servlet-mapping>
2.新建class名字为CodeServlet,代码如下:
/**
*
*/
package com.bbs.logic;
/**
* @author jake
*
*/
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import javax.imageio.*;
import javax.servlet.http.*;
import java.io.*;
public class CodeGenServlet extends HttpServlet {
private static int I_WIDTH = 60;
private static int I_HEIGHT = 18;
private static Font VALIDATECODE_FONT = new Font( "Times New Roman ", Font.PLAIN, 18);
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws javax.servlet.ServletException,IOException {
//在内存中创建图象
BufferedImage image = new BufferedImage(I_WIDTH, I_HEIGHT,
BufferedImage.TYPE_INT_RGB);
//获取图形上下文
Graphics g = image.getGraphics();
//设定背景色
g.setColor(Color.white);
g.fillRect(0, 0, I_WIDTH, I_HEIGHT);
//画边框
g.setColor(Color.blue);
g.drawRect(0, 0, I_WIDTH - 1, I_HEIGHT - 1);
Long validate = new Long(10000 + Math.round((Math.random() * 90000)));
String validateCode=validate.toString();
request.getSession().setAttribute( "validateCode ", validateCode);
//将认证码显示到图象中
g.setColor(Color.black);
g.setFont(VALIDATECODE_FONT);
// g.drawString(rand,10,15);
g.drawString(validateCode.toString(), 7, 15);
//随机产生88个干扰点,使图象中的认证码不易被其它程序探测到
Random random = new Random();
for (int iIndex = 0; iIndex <128; iIndex++) {
int x = random.nextInt(I_WIDTH);
int y = random.nextInt(I_HEIGHT);
g.drawLine(x, y, x, y);
}
//图象生效
g.dispose();
//输出图象到页面
ImageIO.write(image, "JPEG ", response.getOutputStream());
}
}
3.在WebRoot/check目录下checkcode.jsp作为提交验证码表单,里面嵌入引用Servlet.代码:
<%@ page contentType= "text/html;charset=gb2312 " %>
<html>
<head>
<title> 认证码输入页面 </title>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<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= "UrlTest.jsp "> </td>
</tr>
<tr>
<td align=left> 输入上面的认证码: </td>
<td> <input type=text name=rand maxlength=5 value= " "> </td>
</tr>
<tr>
<td colspan=2 align=center> <input type=submit value= "提交检测 "> </td>
</tr>
</form>
</body>
</html>
4。在WebRoot/check目录下check.jsp获取提交的数据,进行判断输入是否与产生的验证码相同:
<%@ page contentType= "text/html; charset=gb2312 " language= "java " import= "java.sql.* " errorPage= " " %>
<html>
<head>
<title> 认证码验证页面 </title>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<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( "validateCode ");
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>
[解决办法]
收下了
[解决办法]
支持!