用servlet实现验证码的切换

来源:互联网 发布:什么是网络服务器 编辑:程序博客网 时间:2024/05/29 13:01
生成随机验证码 用的类如下
1、 Bufferedimage 图片数据缓冲区
2、Graphics绘制图片
3、Color获取颜色
4、Random生成随机数
5、 imager IO输出图片
下面是创建sevlet类
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//创建图片缓冲区 并用bufferedimage 创建graphics 来绘制图片
BufferedImage bi=new BufferedImage(60, 30, BufferedImage.TYPE_USHORT_555_RGB);
//创建绘制图片类
Graphics g=bi.getGraphics();
//获取颜色 进行对缓冲区的图片进行一个绘制颜色
Color c=new Color(181,206,231);
//把颜色放到grahis进行绘制设置绘制边框背景
g.setColor(c);
//绘制边框 坐标想x,y从0开始
g.fill3DRect(0, 0, 60, 30, true);
//toCharArray()就是字符串里的内容转分割换成字符数组,可以遍历输出每一个字母数据
char[]ch="abcdefghijklmnopqsyz1234567890".toCharArray();
//创建随机函数
StringBuffer sb=new StringBuffer();
Random r=new Random();
//获取数组长度,并根据数组的长度进行划分要显示的字符串
int len=ch.length,index;
//for循环遍历数组,随机获取 四个数据
for (int i = 0; i <4; i++) {
//随机回去指定范围内的数据
index=r.nextInt(len);
//绘制其颜色当生成验证码时 都有不同的颜色 所以需要设置随机的颜色
g.setColor(new Color(r.nextInt(88),r.nextInt(111),r.nextInt(255)));
//获取指定范围内的数据后进行绘制数据Graphics类进行绘制,因为获取的数据是字符型还得需要转换成字符串进行显示
//并设置其显示的位置,因为是随机获取四个数据,设置位置时要对数据位置进行间隔,否则都显示到一起挤到一起
g.drawString(ch[index]+"", (i*15)+3, 18);
//把随机的字符串放到StringBuff中
sb.append(ch[index]);
}
//把存储在StringBuff中的数据放到session中也就是把随机的四个数据存储到session中
req.getSession().setAttribute("piccode", sb.toString());
//吧数据写出
ImageIO.write(bi, "jpg", resp.getOutputStream());
1、下面是jsp 表单中的内容代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="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>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function javaimgcode(){
var dates =new Date().getTime();
document.getElementById("img").src="<%=request.getContextPath()%>/servlet/image?d="+dates;
}
</script>
</head>
<body>
<form action="<%=request.getContextPath()%>/servlet/login" method="post">
密码<input type="text">
用户名<input type="text" name="verifyCode" size="6" id="verifyCode">
验证码<input type="text"name="imgname"><img alt="验证码" id="img" src="<%=request.getContextPath()%>/servlet/image">
<a href="javascript:javaimgcode()">看不清换一张</a>
<input type="submit" value="提交">
</form>
</body>
</html>


1 、注 已知 配置好web.xml servlet过滤器
2、当用户输入验证码时 跳转到当前servlet进行判断代码如下:
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=gbk");
//获取随机的那四个数据
String piccode=(String)req.getSession().getAttribute("piccode");
String img=req.getParameter("imgname");
PrintWriter out= resp.getWriter();
if (piccode==img) {
out.println("验证码正确");
}else{
out.println("验证码错误");
}
}

}


0 0
原创粉丝点击