欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用JSP文件生成网站验证码

程序员文章站 2022-07-23 20:12:00
random.(sun企业级应用的首选)文件代码:<%@ page autoflush="false"  import="java.awt.*,java.awt.i...

random.(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);