用JSP文件生成网站验证码

<%@ page autoFlush="false"  import="java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*"%> <%@ page import="org.apache.commons.lang.RandomStringUtils"%> <%  RandomStringUtils rs=new RandomStringUtils(); String random=rs.randomAlphanumeric(4); session.setAttribute("random",random);  %> <%

        out.clear();         response.setContentType("image/jpeg");         response.addHeader("pragma","NO-cache");         response.addHeader("Cache-Control","no-cache");         response.addDateHeader("Expries",0);         int width=100, height=40;         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);         Graphics g = image.getGraphics();         //以下填充背景?色         g.setColor(Color.GRAY);         Font DeFont=new Font("SansSerif", Font.PLAIN, 32);          g.setFont(DeFont);         g.fillRect(0, 0, width, height);        //?置字体?色         g.setColor(Color.RED);         g.drawString(random,3,30);         g.dispose();         ServletOutputStream outStream = response.getOutputStream();         JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(outStream);         encoder.encode(image);         outStream.close();    %>
test.jsp文件代码如下: <%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head>

<body> <img src="random.jsp">  </body> </html>
如果不想导入Apache的包可以自己编译下面这个文件 // FrontEnd Plus GUI for JAD // DeCompiled : RandomStringUtils.class

package org.apache.commons.lang;

import java.util.Random;

public class RandomStringUtils {

    private static final Random RANDOM = new Random();

    public RandomStringUtils()     {     }

    public static String random(int count)     {         return random(count, false, false);     }

    public static String randomAscii(int count)     {         return random(count, 32, 127, false, false);     }

    public static String randomAlphabetic(int count)     {         return random(count, true, false);     }

    public static String randomAlphanumeric(int count)     {         return random(count, true, true);     }

    public static String randomNumeric(int count)     {         return random(count, false, true);     }

    public static String random(int count, boolean letters, boolean numbers)     {         return random(count, 0, 0, letters, numbers);     }

    public static String random(int count, int start, int end, boolean letters, boolean numbers)     {         return random(count, start, end, letters, numbers, null);     }

    public static String random(int count, int start, int end, boolean letters, boolean numbers, char set[])     {         if(start == 0 && end == 0)         {             end = 122;             start = 32;             if(!letters && !numbers)             {                 start = 0;                 end = 0x7fffffff;             }         }         StringBuffer buffer = new StringBuffer();         int gap = end - start;         while(count-- != 0)          {             char ch;             if(set == null)                 ch = (char)(RANDOM.nextInt(gap) + start);             else                 ch = set[RANDOM.nextInt(gap) + start];             if(letters && numbers && Character.isLetterOrDigit(ch) || letters && Character.isLetter(ch) || numbers && Character.isDigit(ch) || !letters && !numbers)                 buffer.append(ch);             else                 count++;         }         return buffer.toString();     }

    public static String random(int count, String set)     {         return random(count, set.toCharArray());     }

    public static String random(int count, char set[])     {         return random(count, 0, set.length - 1, false, false, set);     }

}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。