用JSP文件生成网站验证码
random.jsp(SUN企业级应用的首选)文件代码:
<%@ page autoFlush="false" import="java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*"%>
<%@ page import="org.apache(Unix平台最流行的WEB服务器平台).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(SUN企业级应用的首选)文件代码如下:
<%@ 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(SUN企业级应用的首选)">
</body>
</html>
如果不想导入apache(Unix平台最流行的WEB服务器平台)的包可以自己编译下面这个文件
// FrontEnd Plus GUI for JAD
// DeCompiled : RandomStringUtils.class
package org.apache(Unix平台最流行的WEB服务器平台).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);
相关新闻>>
- 发表评论
-
- 最新评论 更多>>