servlet

package com.htpo.net;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class testget extends HttpServlet {

/**
     * Constructor of the object.
     */
    public testget() {
        super();
    }

public static final char[] CHARS = { '2', '3', '4', '5', '6', '7', '8',
            '9', '0', 'A', 'Q', 'W', 'E', 'R', 'T', 'G', 'D', 'S', 'W', 'G',
            'H' };// 随机字符的字典
    public static Random random = new Random();// 随机数

public static String getRandomString() {
        // 字符的缓存
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < 6; i++) {// 循环 六次
            buf.append(CHARS[random.nextInt(CHARS.length)]);
        }
        return buf.toString();
    }

public static Color getRandomColor() {
        return new Color(random.nextInt(255), random.nextInt(255),
                random.nextInt(255));
    }

public static Color getReverseColor(Color c) {
        return new Color(255 - c.getRed(), 255 - c.getGreen(),
                255 - c.getBlue());
    }

/**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

/**
     * 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"); String re =
         * request.getParameter("username"); System.out.println(re); PrintWriter
         * out = response.getWriter();
         *
         * // response.sendRedirect("test.jsp"); // String s =
         * "[{ id:1, pId:0, name:"pNode
         * 1", open:true},{ id:11, pId:1, name:"pNode
         * 11"},{ id:111, pId:11, name:"leaf node
         * 111"},{ id:112, pId:11, name:"leaf node
         * 112"},{ id:113, pId:11, name:"leaf node
         * 113"},{ id:114, pId:11, name:"leaf node
         * 114"},{ id:12, pId:1, name:"pNode 12"},{ id:121, pId:12, name:"leaf
         * node 121"},{ id:122, pId:12, name:"leaf node
         * 122"},{ id:123, pId:12, name:"leaf node
         * 123"},{ id:124, pId:12, name:"leaf node
         * 124"},{ id:13, pId:1, name:"pNode 13 - no
         * child", isParent:true},{ id:2, pId:0, name:"pNode
         * 2"},{ id:21, pId:2, name:"pNode
         * 21", open:true},{ id:211, pId:21, name:"leaf node
         * 211"},{ id:212, pId:21, name:"leaf node
         * 212"},{ id:213, pId:21, name:"leaf node
         * 213"},{ id:214, pId:21, name:"leaf node
         * 214"},{ id:22, pId:2, name:"pNode 22"},{ id:221, pId:22, name:"leaf
         * node 221"},{ id:222, pId:22, name:"leaf node
         * 222"},{ id:223, pId:22, name:"leaf node
         * 223"},{ id:224, pId:22, name:"leaf node
         * 224"},{ id:23, pId:2, name:"pNode 23"},{ id:231, pId:23, name:"leaf
         * node 231"},{ id:232, pId:23, name:"leaf node
         * 232"},{ id:233, pId:23, name:"leaf node
         * 233"},{ id:234, pId:23, name:"leaf node
         * 234"},{ id:3, pId:0, name:"pNode 3 - no child", isParent:true}]";
         * out.write(s);
         *
         * out.println(
         * "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
         * out.println("<HTML>");
         * out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
         * out.println("  <BODY>"); out.print("    This is ");
         * out.print(this.getClass()); out.println(", using the GET method");
         * out.println("  </BODY>"); out.println("</HTML>");
         *
         * out.flush(); out.close();
         */
        String s = request.getParameter("username");
        System.out.println(s);
        response.setContentType("image/jpeg");// 设置输出的类型
        String randomString = getRandomString();// 得到返回的字符集
        request.getSession(true).setAttribute("randomString", randomString);
        int with = 100;
        int hight = 30;// 生成图片的大小
        Color color = getRandomColor();// 用于背景色
        Color reverse = getReverseColor(color);// 用于前景色
        BufferedImage bi = new BufferedImage(with, hight,
                BufferedImage.TYPE_INT_RGB);// 创建一个彩色的图片
        Graphics2D g = bi.createGraphics();// 获取到绘图对象
        g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));// 设置字体
        g.setColor(color);// 设置颜色
        g.fillRect(0, 0, with, hight);// 绘制背景
        g.setColor(reverse);// 设置颜色
        g.drawString(randomString, 18, 20);// 绘制随机字符
        for (int i = 0, n = random.nextInt(100); i < n; i++) {// 画最多100个噪音点
            g.drawRect(random.nextInt(with), random.nextInt(hight), 1, 1);// 随机噪音点
        }
        ServletOutputStream out = response.getOutputStream();
        // 转换图片格式
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(bi);// 对图片进行编码
        out.flush();// 输出
    }

/**
     * 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
     */

/**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException
     *             if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}
---------------------------------------

jsp

function reloadImage(){
            document.getElementByIdx_x_x("btn").disabled=false;
            document.getElementByIdx_x_x("Identity").src='testget?ts='+new Date().getTime();
            
         }
         
         function yanzheng() {
         
            
        }
         
    </script>

<img src="testget" id="Identity" />
   <input type="button" id="btn" value="换个图片" onclick="reloadImage()"/>

servlet 验证码生成的更多相关文章

  1. servlet中生成验证码

    在servlet中生成验证码 package login; import java.awt.Color; import java.awt.Graphics; import java.awt.image ...

  2. Web---图片验证码生成教程详解-从简单到复杂-从本地到前后台

    首先,我们先来看本地如何生成图片验证码的,再来写输出到网页的验证码如何实现. 先来看最简单的-实现的功能是,将一个字符串变成图片写入到文件中 实现代码: package cn.hncu.img; im ...

  3. 利用谷歌 kaptcha 进行验证码生成

    package main.com.smart.controller; import com.google.code.kaptcha.Producer; import main.com.smart.ut ...

  4. 轻量级验证码生成插件webutil-licenseImage

    轻量级验证码生成插件webutil-licenseImage源码与实例应用   webutil-licenseImage 插件内置4种验证码样式,支持用户扩展.自定义样式实现简单验证码. 源码脱管地址 ...

  5. JAVA 验证码生成(转)

    最近做了一下验证码的功能,网上找了一篇还不错,引用下:http://blog.csdn.net/ruixue0117/article/details/22829557 这篇文章非常好,但是web和js ...

  6. Web后端 JAVA实现验证码生成与验证功能

    首先,写一个验证码生成帮助类,用来绘制随机字母: <span style="font-size:14px;">import java.awt.Color;  impor ...

  7. java web中验证码生成的demo

    首先创建一个CaptailCode类 package com.xiaoqiang.code; import java.awt.*; import java.awt.font.FontRenderCon ...

  8. JavaWeb开发之普通图片验证码生成技术与算术表达式验证码生成技术

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6134649.html    另:算术验证码生成的JSP.Servlet实现均已移植github:https:/ ...

  9. 轻量级验证码生成插件webutil-licenseImage源码与实例应用

    webutil-licenseImage 插件内置4种验证码样式,支持用户扩展.自定义样式实现简单验证码. 源码脱管地址: http://code.google.com/p/licenseimage/ ...

随机推荐

  1. 非mapreduce生成Hfile,然后导入hbase当中

    转自:http://blog.csdn.net/stark_summer/article/details/44174381 未实验 最近一个群友的boss让研究hbase,让hbase的入库速度达到5 ...

  2. C#深入解析委托——C#中为什么要引入委托

    引言: 对于一些刚接触C# 不久的朋友可能会对C#中一些基本特性理解的不是很深,然而这些知识也是面试时面试官经常会问到的问题,所以我觉得有必要和一些接触C#不久的朋友分享下关于C#基础知识的文章,所以 ...

  3. (转)C# WebApi 接口参数不再困惑:传参详解

    原文地址:https://www.cnblogs.com/landeanfen/p/5337072.html 本篇打算通过get.post.put.delete四种请求方式分别谈谈基础类型(包括int ...

  4. QQ号码正则判断

    <!DOCTYPE html> <html>     <head>         <meta charset="UTF-8">   ...

  5. 2-Zookeeper、HA安装

    1.Zookeeper安装 1.解压 zookeeper 到安装目录中/opt/app/zookeeper 中. 2.在安装目录下创建data和logs两个目录用于存储数据和日志: cd /opt/a ...

  6. JVM总结-java对象的内存布局

    在 Java 程序中,我们拥有多种新建对象的方式.除了最为常见的 new 语句之外,我们还可以通过反射机制.Object.clone 方法.反序列化以及 Unsafe.allocateInstance ...

  7. web前端全栈学习之路

    web前端全栈学习之路 --- 陆续更新中 一.HTML相关 1.HTML常用标签:http://www.cnblogs.com/wyb666/p/8733699.html 2.HTML5基础: 3. ...

  8. 第十届蓝桥杯JavaB组总结

    去年参加了第九届蓝桥杯C/C++B组,很捞,做了大概5道题,就好像就做对了2道结果填空题,编程题只做了一个(只通过了部分测试数据),最后拿了个省三,但是班上那些平时没有认真准备的都拿了省二 今年想好好 ...

  9. SVG 学习<六> SVG的transform

    目录 SVG 学习<一>基础图形及线段 SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组 SVG 学习<三>渐变 SVG 学习<四 ...

  10. nine

    [拉肚子] 1.原因:消化不良.不良刺激.进食不当的食物/病毒 2.调理: 消化不良引起的:妈咪爱:山药粥:少食多餐 不良刺激引起的:不要受惊受凉过热