生成验证码的基本过程是:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.awt.*,java.awt.image.BufferedImage,javax.imageio.ImageIO" %>
<%
response.setHeader("Cache-Control", "no-cache");
int width=60,height=20;
<!--实例化bufferdImage对象-->
BufferedImage image=new BufferedImage( width,height,BufferedImage.TYPE_INT_RGB);
//由bufferdImage对象获得画笔g
Graphics g=image.getGraphics();
//画笔填充矩形
g.setColor(new Color(200,200,200));
g.fillRect(0, 0, width, height);
//生成随机数
Random rd=new Random();
int rdNum=rd.nextInt(8999)+1000;
String rdstr=String.valueOf(rdNum);
//把随机数放入session中
session.setAttribute("rdStr", rdstr);
//画笔g画随机数
g.setColor(Color.BLUE);
g.setFont(new Font("",Font.PLAIN,20));
g.drawString(rdstr, 10, 20);//这里的坐标是一个字符的左下角基线
//画100个随机点
g.setColor(Color.green);
for(int i=0;i<100;i++){
int x=rd.nextInt(width);
int y=rd.nextInt(height);
g.drawOval(x, y, 1, 1);
}
//用ImageIO的静态函数write输出图像
ImageIO.write(image, "JPEG", response.getOutputStream());
out.clear();
out=pageContext.popBody();
%>
画图的基本思想是:
<!--loginform.jsp登录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 ‘loginform.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">
-->
</head>
<body>
<script type="text/javascript">
function refresh(){ document.loginform.imgcode.src="codes.jsp";
}
</script>
<form name="loginform" action="/code/servlets/codeServlet" method="post">
<h4 align="center"><b>欢迎登陆</b></h4><br>
<table align="center" border="1">
<tr>
<td align="right">请输入账号:</td>
<td><input name="zhanghao" type="text" > </td>
</tr>
<tr>
<td align="right">请输入密码:</td>
<td><input name="mima" type="password" size="21" > </td>
</tr>
<tr>
<td align="right">请输入验证码:</td>
<td ><input name="yanzhengma" type="text" size="10" ><img name="imgcode" src="codes.jsp" onclick="refresh()"> </td><!--验证码当图片处理,点击可刷新-->
</tr>
<tr>
<td></td>
<td><input type="submit" value="登录" > <input type="button" value="忘记密码?"></td>
</tr>
</table>
</form>
</body>
</html>
<!--codeServlet.java-->
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class codeServlet extends HttpServlet {
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取表单的验证码
String code=request.getParameter("yanzhengma");
//获取session中的验证码
HttpSession session=request.getSession();
String randstr=(String) session.getAttribute("rdStr");
response.setCharacterEncoding("gb2312");//respons设为中文编码
PrintWriter out=response.getWriter();//获得输出写出器
if(code.equals(randstr)){//输入的验证码和session保存的验证码相比较
out.println("恭喜你,验证码正确!");
}
else{
//response.sendRedirect("/code/back.jsp");
out.println("验证码错误!!");
}
}
}
输入结果是:
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/tuke_tuke/article/details/47211329