动态生成验证码
来源:互联网 发布:怎么理解舍伍德算法 编辑:程序博客网 时间:2024/04/29 14:26
package com.thinkbank.code;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class randomCode extends HttpServlet {
private int width = 60;
private int height = 30;
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
//创建一个随即数生成器
Random random = new Random();
g.setColor(Color.WHITE);
g.fillRect(0,0,width,height);
//创建字体,字体的大小应该根据图片的高度来确定。
Font font = new Font("Times New Roman",Font.PLAIN,18);
//设置字体
g.setFont(font);
//画边框
g.setColor(Color.BLACK);
g.drawRect(0,0,width-1,height-1);
//随即产生160条干扰线,使程序中的验证码不易被其他程序探测到
g.setColor(Color.GRAY);
for (int i=0;i<160;i++){
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(12);
int y1 = random.nextInt(12);
g.drawLine(x,y,x+x1,y+y1);
}
//randomcode用于保存随即产生的验证码,以便于用户登陆后进行验证
StringBuffer randomcode = new StringBuffer();
int red = 0;
int green = 0;
int blue = 0;
//随即产生4位数的验证码
for (int i=0;i<4;i++){
//得到随即产生的验证码数字
String str = String.valueOf(random.nextInt(10));
//产生随即的颜色分量来构造颜色值,这样输出的每位数字的颜色值都不相同
red = random.nextInt(110);
green = random.nextInt(50);
blue = random.nextInt(50);
//随即产生的颜色将验证码绘制到图象中
g.setColor(new Color(red,green,blue));
g.drawString(str,13*i+6,16);
//将产生的4个随即数组合在一起
randomcode.append(str);
}
//将四位数字的验证码保存到session中
HttpSession session = arg0.getSession();
session.setAttribute("randomcode",randomcode.toString());
//禁止图象缓存
arg1.setHeader("Pragma","no-cache");
arg1.setHeader("Cache-Control","no-cache");
arg1.setDateHeader("Expires",0);
arg1.setContentType("image/jpeg");
//将图象输出到servlet输出流中
ServletOutputStream sos = arg1.getOutputStream();
ImageIO.write(image,"jpeg",sos);
sos.close();
}
public randomCode() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
package com.thinkbank.code;
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 logincheck extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @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 doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
}
/**
* 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 {
response.setContentType("text/html;charset=gbk");
request.setCharacterEncoding("gbk");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String randomCode = (String) session.getAttribute("randomcode");
if (randomCode == null){
response.sendRedirect("login.jsp");
return ;
}
String reqRandom = request.getParameter("random");
if (randomCode.equals(reqRandom)){
out.println("恭喜你,输入正确!");
}else{
out.println("输入错误,请返回重新输入!");
}
out.close();
}
}
<%@ 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>登陆页面</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>
<FORM action="/RandomCodeServlet/logincheck" method="post">
<table>
<tr>
<td>用户名:</td>
<td><INPUT type="text" name="username"></td>
</tr>
<tr>
<td>密 码:</td>
<td><INPUT type="password" name="password"></td>
</tr>
<tr>
<td>验证码:</td>
<td><INPUT type="text" name="random" maxlength="4"><IMG src="randomCode"></td>
</tr>
<tr>
<td><INPUT type="reset" value="重填"></td>
<td><INPUT type="submit" value="提交"></td>
</tr>
</table>
</FORM>
</body>
</html>
- 动态生成验证码
- 动态生成验证码
- 动态生成验证码
- 动态生成验证码
- 动态生成验证码
- 动态生成验证码
- 动态生成验证码图片
- 随机生成动态验证码
- Servlet生成动态验证码
- java动态生成验证码
- jsp生成动态验证码
- 【PHP】动态生成验证码
- 如果生成动态验证码
- PHP动态生成验证码
- java生成动态验证码
- 动态验证码生成实例
- 随机生成动态验证码
- Servlet生成动态验证码
- 商业搜索引擎大行其道 未来规模将超越ERP
- 用mod_security增强web安全性(下)
- AWStats: Apache/IIS的日志分析工具
- give me a reason to use this blog...
- ARM Boot示例
- 动态生成验证码
- 简述U-boot的修改
- TechCrunch:生活中必不可少的Web2.0公司
- csdn faster than blogbus...
- java的数据结构
- 韩国网站风格设计欣赏
- IntrospectorCleanupListener
- 如何做自己的无限二级域名系统程序
- me: the past year