code

jsp:::

package com.zw.fantasticblog.util;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.imageio.*;
public class ImageServlet extends HttpServlet {
 public void init() throws ServletException
 {
  // Put your code here
 }
 public ImageServlet()
 {
  super();
 }
    static final int WIDTH = 100, HEIGHT = 30;
    final Random rnd = new Random();

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws
            ServletException, IOException {
        String num;
        String s;
        String snew;
        String num2=null;
        num = String.valueOf(rnd.nextInt(9000)+1000);
        //既可以数字也可以是字符
        for(int i=0;i<4;i++)
        {
            int n=rnd.nextInt(3);
            s=num.substring(n,n+1);
            char[] ch={
                      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                      'L', 'M', 'N', 'O', 'P', 'Q','R','S','T','U','V','W','X',
                      'Y','Z'
            };
            int j=(int)(Math.random()*26);
            char c=ch[j];
            snew=String.valueOf(c);
            num2=num.replace(s,snew);
        }
        //把验证码设到session中
        HttpSession session=request.getSession();
        session.setAttribute("validateCode",num2);
//        System.out.println(session.getId());
       
        //System.out.println(num);
        //以图像形式发送给客户端
        response.setContentType("image/jpeg");
        this.createImage(response.getOutputStream(),num2);
    }

    private void createImage(OutputStream out,String num2) {
        //构造一幅图像
        BufferedImage bi = new BufferedImage(WIDTH, HEIGHT,
                                             BufferedImage.TYPE_USHORT_555_RGB);
        Graphics2D g = bi.createGraphics();
        // 背景
        g.setColor(new Color(200, 200, 200));
        g.fillRect(0, 0, WIDTH, HEIGHT);
        // 加入随机背景
        randomBackground(g, 25);
        // 显示验证码
        //g.setColor(Color.WHITE);
        Font t = new Font("黑体", Font.BOLD, 30);
        g.setFont(t);
        Color c;
        for (int i = 0; i < num2.length(); i++) {
            c = getRandomColor();
            g.setColor(c);
            g.drawString(num2.substring(i, i + 1), 10 + 20 * i, 25);
        }
        try {
         //放到输出流中
            ImageIO.write(bi, "JPEG", out);
            out.flush();
            out.close();
        } catch (IOException ex) {
            //此处不用处理
        }
    }
    //画背景的圆
    private void randomBackground(Graphics2D g, int count) {
        int x1, y1, r;
        Color c;
        //画小椭圆
        for (int i = 0; i < count; i++) {
            x1 = rnd.nextInt(WIDTH);
            y1 = rnd.nextInt(HEIGHT);
            r = rnd.nextInt(5)+1;
            c = getRandomColor();
            g.setColor(c);
            g.fillOval(x1, y1, r, r);
        }
        //又画了很多斜线
        g.setColor(getRandomColor());  
        for(int   i=0;i<155;i++)  
        {  
            int   x   =   rnd.nextInt(WIDTH);  
            int   y   =   rnd.nextInt(HEIGHT);  
            int   xl   =   rnd.nextInt(12);  
            int   yl   =   rnd.nextInt(12);  
            g.drawLine(x,y,x+xl,y+yl);  
        }  

    }

    private Color getRandomColor()
    {
        return new Color(rnd.nextInt(200), rnd.nextInt(200), rnd.nextInt(200));
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws
            ServletException, IOException {

        this.doGet(request, response);
    }
    public void destroy()
 {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }

}

html:

<script language="javascript">
function changeImage(img)
{
 img.src="/Fantastic blog/imageservlet?=newid"+Math.random();
}
</script>

<body οnlοad="changeImage(loginForm.vc)">

 <tr>
          <td><div align="right" class="style1"> 验 &nbsp;证 &nbsp;码:</div></td>
          <td>
           <html:text property="validateCode" styleClass="style2"/>&nbsp;&nbsp;&nbsp;<img alt="请刷新" name="vc" src="/Fantastic blog/imageservlet?newid=a" />
           <input type="button" οnclick="changeImage(vc);" value="看不清楚?"><br><html:errors property="validateCode"/></td>
        </tr>

 

//paging

document.getElementById('selects').value=${currentPage};
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值