日期:2014-05-17  浏览次数:20686 次

怎么做验证码功能呢
我现在正在做一个项目,主要用的框架是Struts
其中要做验证码的要怎么做呢
谁能给个杨例给我看看

------解决方案--------------------
HTML code

<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!
Color getRandColor(int fc,int bc){//给定范围获得随机颜色
        Random random = new Random();
        if(fc>255) fc=255;
        if(bc>255) bc=255;
        int r=fc+random.nextInt(bc-fc);
        int g=fc+random.nextInt(bc-fc);
        int b=fc+random.nextInt(bc-fc);
        return new Color(r,g,b);
        }
%>
<%
//设置页面不缓存
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);

// 在内存中创建图象
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

// 获取图形上下文
Graphics g = image.getGraphics();

//生成随机类
Random random = new Random();

// 设定背景色
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);

//设定字体
g.setFont(new Font("Times New Roman",Font.PLAIN,18));

//画边框
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);


// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
        int xl = random.nextInt(12);
        int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}

// 取随机产生的认证码(4位数字)
String sRand="";
for (int i=0;i<4;i++){
    String rand=String.valueOf(random.nextInt(10));
    sRand+=rand;
    // 将认证码显示到图象中
    g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
    g.drawString(rand,13*i+6,16);
}

// 将认证码存入SESSION
session.setAttribute("rand",sRand);


// 图象生效
g.dispose();

// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
%>

------解决方案--------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JPasswordFieldDemo extends JFrame {
JTextField username; //用户名输入框
JPasswordField password; //密码输入框
JButton logonButton; //登录按钮
JButton cancelButton; //退出按钮

public JPasswordFieldDemo() { //构造函数

super("JPasswordField演示"); //调用父类构造函数
Container container=getContentPane(); //得到容器
container.setLayout(new GridLayout(3, 2, 2, 2)); //设置布局管理器

username=new JTextField(16); //初始化文本输入框,宽度为16列
password=new JPasswordField(16); //初始化密码输入框,宽度为16列
logonButton=new JButton("登录"); //初始化登录按钮
logonButton.addActionListener( //登录按钮事件处理
new ActionListener(){
public void actionPerformed(ActionEvent evt){
char[] pw=password.getPassword(); //得到密码
String message="您的用户名:"+username.getText()+"\n您的密码:"+new String(pw); //消息字符串
JOptionPane.showMessageDialog(JPasswordFieldDemo.this, message); //显示消息
}
});
cancelButton=new JButton("退出"); //初始化退出按钮
cancelButton.addActionListener( //初始化按钮事件处理
new ActionListener(){
public void actionPerformed(ActionEvent evt){
System.exit(0); //退出程序
}
});

container.add(new JLabel(" 用户名:")); //增加组件
container.add(username);
container.add(new JLabel(" 密码:"));
container.add(password);
container.add(logonButton);
container.add(cancelButton);
setResizable(false); //不允许用户改变窗口大小
setSize(300,120); //设置窗口尺寸
setVisible(true); //设置窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
  }

  public static void mai