1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
  2.  
  3. <%
  4. String action = request.getParameter("action");
  5. String safecodeText = request.getParameter("safecodeTest");
  6. if("action".equals(action)){
  7. String safecode = (String)session.getAttribute("safecode");
  8. if(safecode.equals(safecodeText)){
  9. out.print("验证码正确!");
  10. }else{
  11. out.print("验证码错误!请重新输入!");
  12. }
  13. }
  14. %>
  15. <html>
  16. <head>
  17. <title>验证码测试</title>
  18. </head>
  19.  
  20. <body>
  21. <form action="servlet/demo" method="post">
  22. <input type="hidden" name="action" value="action"/>
  23. <img alt="验证码" src="servlet/demo">
  24. <input type="text" name="safecodeTest">
  25. <input type="submit" value="go">
  26. </form>
  27. </body>
  28. </html>
  1. import java.io.*;
  2.  
  3. import javax.servlet.*;
  4. import javax.servlet.http.*;
  5. import java.util.Random;
  6. import java.awt.*;
  7. import java.awt.image.*;
  8. import javax.imageio.*;
  9.  
  10. public class demo extends HttpServlet {
  11. //产生随即的字体
  12. private Font getFont() {
  13. Random random = new Random();
  14. Font font[] = new Font[5];
  15. font[0] = new Font("Ravie", Font.PLAIN, 24);
  16. font[1] = new Font("Antique Olive Compact", Font.PLAIN, 24);
  17. font[2] = new Font("Forte", Font.PLAIN, 24);
  18. font[3] = new Font("Wide Latin", Font.PLAIN, 24);
  19. font[4] = new Font("Gill Sans Ultra Bold", Font.PLAIN, 24);
  20. return font[random.nextInt(5)];
  21. }
  22.  
  23. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  24. throws ServletException, IOException {
  25. // 设置响应头 Content-type类型
  26. resp.setContentType("image/jpeg");
  27. // 以下三句是用于设置页面不缓存
  28. resp.setHeader("Pragma", "No-cache");
  29. resp.setHeader("Cache-Control", "No-cache");
  30. resp.setDateHeader("Expires", 0);
  31.  
  32. OutputStream os = resp.getOutputStream();
  33. int width = 83, height = 30;
  34. // 建立指定宽、高和BufferedImage对象
  35. BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
  36.  
  37. Graphics g = image.getGraphics(); // 该画笔画在image上
  38. Color c = g.getColor(); // 保存当前画笔的颜色,用完画笔后要回复现场
  39. g.fillRect(0, 0, width, height);
  40.  
  41. char[] ch = "abcdefghjkmnpqrstuvwxyz23456789".toCharArray(); // 随即产生的字符串 不包括 i l(小写L) o(小写O) 1(数字1)0(数字0)
  42. int length = ch.length; // 随即字符串的长度
  43. String sRand = ""; // 保存随即产生的字符串
  44. Random random = new Random();
  45. for (int i = 0; i <; i++) {
  46. // 设置字体
  47. g.setFont(getFont());
  48. // 随即生成0-9的数字
  49. String rand = new Character(ch[random.nextInt(length)]).toString();
  50. sRand += rand;
  51. // 设置随机颜色
  52. g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
  53. g.drawString(rand, 20 * i + 6, 25);
  54. }
  55. //产生随即干扰点
  56. for (int i = 0; i < 20; i++) {
  57. int x1 = random.nextInt(width);
  58. int y1 = random.nextInt(height);
  59. g.drawOval(x1, y1, 2, 2);
  60. }
  61. g.setColor(c); // 将画笔的颜色再设置回去
  62. g.dispose();
  63.  
  64. //将验证码记录到session
  65. req.getSession().setAttribute("safecode", sRand);
  66. // 输出图像到页面
  67. ImageIO.write(image, "JPEG", os);
  68.  
  69. }
  70.  
  71. protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  72. throws ServletException, IOException {
  73. doGet(req, resp);
  74. }
  75.  
  76. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  3. <display-name>JavaWebDemo</display-name>
  4. <servlet>
  5. <description>This is the description of my J2EE component</description>
  6. <display-name>This is the display name of my J2EE component</display-name>
  7. <servlet-name>demo</servlet-name>
  8. <servlet-class>demo</servlet-class>
  9. </servlet>
  10.  
  11. <servlet-mapping>
  12. <servlet-name>demo</servlet-name>
  13. <url-pattern>/servlet/demo</url-pattern>
  14. </servlet-mapping>
  15.  
  16. <welcome-file-list>
  17. <welcome-file>index.html</welcome-file>
  18. <welcome-file>index.htm</welcome-file>
  19. <welcome-file>index.jsp</welcome-file>
  20. <welcome-file>default.html</welcome-file>
  21. <welcome-file>default.htm</welcome-file>
  22. <welcome-file>default.jsp</welcome-file>
  23. </welcome-file-list>
  24. </web-app>
  1. package action;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.image.BufferedImage;
  7. import java.io.ByteArrayInputStream;
  8. import java.io.ByteArrayOutputStream;
  9. import java.util.Map;
  10.  
  11. import javax.imageio.ImageIO;
  12. import javax.imageio.stream.ImageOutputStream;
  13.  
  14. import com.opensymphony.xwork2.ActionContext;
  15. import com.opensymphony.xwork2.ActionSupport;
  16.  
  17. public class ImageAction extends ActionSupport {
  18.  
  19. /**
  20. *
  21. */
  22. private static final long serialVersionUID = 1L;
  23. private ByteArrayInputStream inputStream;
  24. public String createRandomString(){
  25. String str="";
  26. for(int i=0;i<4;i++){
  27. str+=Integer.toString((new Double(Math.random()*10)).intValue());
  28. }
  29. return str;
  30. }
  31. public Color createRandomColor(){
  32. int r=(new Double(Math.random()*256)).intValue();
  33. int g=(new Double(Math.random()*256)).intValue();
  34. int b=(new Double(Math.random()*256)).intValue();
  35. return new Color(r,g,b);
  36. }
  37. public BufferedImage createImage(String str){
  38. int width=60;
  39. int height=22;
  40. BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
  41. Graphics g=image.getGraphics();
  42. g.setColor(Color.WHITE);
  43. g.fillRect(0, 0, width, height);
  44. g.setColor(Color.BLACK);
  45. g.drawRect(0, 0, width-1, height-1);
  46. g.setFont(new Font("Atlantic Inline",Font.PLAIN,18));
  47. g.setColor(this.createRandomColor());
  48. g.drawString(Character.toString(str.charAt(0)), 8, 17);
  49. g.drawString(Character.toString(str.charAt(1)), 20, 17);
  50. g.drawString(Character.toString(str.charAt(2)), 33, 17);
  51. g.drawString(Character.toString(str.charAt(3)), 45, 17);
  52. g.dispose();
  53. return image;
  54. }
  55. public ByteArrayInputStream createInputStream() throws Exception{
  56. String str=this.createRandomString();
  57. BufferedImage image=this.createImage(str);
  58. ActionContext actioncontext=ActionContext.getContext();
  59. Map session=actioncontext.getSession();
  60. session.put("random", str);
  61. ByteArrayOutputStream output=new ByteArrayOutputStream();
  62. ImageOutputStream imageout=ImageIO.createImageOutputStream(output);
  63. ImageIO.write(image, "JPEG", imageout);
  64. imageout.close();
  65. ByteArrayInputStream input=new ByteArrayInputStream(output.toByteArray());
  66. output.close();
  67. return input;
  68. }
  69. public String execute() throws Exception{
  70. setInputStream(createInputStream());
  71. return SUCCESS;
  72. }
  73. public ByteArrayInputStream getInputStream(){
  74. return inputStream;
  75. }
  76. public void setInputStream(ByteArrayInputStream inputStream){
  77. this.inputStream=inputStream;
  78. }
  79. }

利用servlet技术实现验证码功能的更多相关文章

  1. django验证码功能

    1.目的 现在我们一般访问网页都需要输入验证码,比如博客园,有的甚至是通过手机验证码实时登录.这样做的目的主要还是为了防止其他人的恶意访问,比如爬虫,下面就来看看验证码是如何实现的 2.StringI ...

  2. Servlet案例3:验证码功能

    这里介绍简单的验证码功能 动态生成图片 一个简单的页面: <!DOCTYPE html> <html> <head> <meta charset=" ...

  3. 利用PHP绘图函数实现简单验证码功能

    index.php __________________________________________________________________________________________ ...

  4. c#实现验证码功能

    一.验证码简介 验证码功能一般是用于防止批量注册的,不少网站为了防止用户利用机器人自动注册.登录.灌水,都采用了验证码技术.所谓验证码,就是将一串随机产生的数字或字母或符号或文字,生成一幅图片, 图片 ...

  5. javaweb实现验证码功能

    在javaweb的用户注册与登陆功能时,有时为了防止漏洞或者大量注册,可以使用验证码功能,下面是验证码的一个简单实现 验证码类 public class ValiImg extends HttpSer ...

  6. 用PHP实现验证码功能

    目前,不少网站为了防止用户利用机器人自动注册.登录.灌水,都采用了 验证码技术.所谓验证码,就是将一串随机产生的数字或符号,生成一幅图片, 图片里加上一些干扰象素(防止OCR),由用户肉眼识别其中的验 ...

  7. 用java来实现验证码功能(本帖为转载贴),作为个人学习收藏用

    一.关于为何使用验证的解释 在目前的网页的登录.注册中经常会见到各种验证码.其目的便是为了:防止暴力破解  .因为只要CPU性能较强,便可以在慢慢尝试密码的过程中来破解用户账号,因而导致的结果是用户信 ...

  8. c#实现验证码功能(多种模式下分别实现验证功能)详细,带注释

    网上找了很多验证相关的代码,发现有很多瑕疵.现在本人整理测试了一个实现验证码功能的代码,里面有纯数字,纯英文,英文和数字混合等三种模式.并且在必要地方都已经备有注释,希望可以帮到那些需要的人. 验证码 ...

  9. S2SH框架中的无刷新验证码功能实现

    暑假期间在实验室做使用S2SH框架的项目,其中登录和注册需要验证码,实现了一个没有实现刷新验证码功能的简单版本,代码如下: 1 package com.sem.action; 2 3 import j ...

随机推荐

  1. 基于WebForm+EasyUI的业务管理系统形成之旅 -- 总体介绍

    一.系统总体介绍 企业业务管理系统是针对经营企业管理而开发的专业管理软件, 是以“精细管理.过程监控”为设计理念,全面满足企业的信息化管理需求,充分发挥专业.平台.灵活等优点. 集进销存.财务.CRM ...

  2. 生产环境下JAVA进程高CPU占用故障排查

    问题描述:生产环境下的某台tomcat7服务器,在刚发布时的时候一切都很正常,在运行一段时间后就出现CPU占用很高的问题,基本上是负载一天比一天高. 问题分析:1,程序属于CPU密集型,和开发沟通过, ...

  3. lightoj 1007

    预先处理好phi数组和前缀和,水题. #include<cstdio> #include<string> #include<cstring> #include< ...

  4. apache学习

    核心功能和多路处理模块: Core:apache HTTP服务器核心提供的功能,始终有效 Mpm_common:收集了被多个多路处理模块(MPM)实现的公共指令 其他普通模块: mod_actions ...

  5. C#实现Zip压缩解压实例【转】

    本文只列举一个压缩帮助类,使用的是有要添加一个dll引用ICSharpCode.SharpZipLib.dll[下载地址]. 另外说明一下的是,这个类压缩格式是ZIP的,所以文件的后缀写成 .zip. ...

  6. ActiveMQ的安全机制使用及其源代码分析 [转]

    ActiveMQ是目前较为流行的一款开源消息服务器.最近在项目开发中,需要为ActiveMQ开发基于IP的验证和授权机制,因此,对ActiveMQ的安全机制进行了了解,以下将介绍ActiveMQ的安全 ...

  7. Git branch (分支学习)

    以前总结的一些git操作,分享在这里. Git 保存的不是文件差异或者变化量,而只是一系列文件快照.   - 列出当前所有分支 git branch <--merge> | <--n ...

  8. hbase单机环境的搭建和完全分布式Hbase集群安装配置

    HBase 是一个开源的非关系(NoSQL)的可伸缩性分布式数据库.它是面向列的,并适合于存储超大型松散数据.HBase适合于实时,随机对Big数据进行读写操作的业务环境. @hbase单机环境的搭建 ...

  9. vlookup使用案例

    http://www.360doc.com/content/13/1119/20/9842991_330586745.shtml

  10. HTTP 缓存控制总结

    引言 通过网络获取内容既缓慢,成本又高:大的响应需要在客户端和服务器之间进行多次往返通信,这拖延了浏览器可以使用和处理内容的时间,同时也增加了访问者的数据成本.因此,缓存和重用以前获取的资源的能力成为 ...