用JSP檔案產生網站驗證碼

來源:互聯網
上載者:User
js|驗證碼 random.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);
}

}




相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。