怎么做验证码功能呢 我现在正在做一个项目,主要用的框架是Struts其中要做验证码的要怎么做呢谁能给个杨例给我看看 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 代码比较多,我就不帖了。参考我们教程中的例子:http://www.family168.com/tutorial/jsp/html/jsp-ch-12.html <%@ 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);}// 将认证码存入SESSIONsession.setAttribute("rand",sRand);// 图象生效g.dispose();// 输出图象到页面ImageIO.write(image, "JPEG", response.getOutputStream());%> 要用到java 的图形界面,一般都到网上找个现成的,不会自己去写 if xxx==xxx的问题,关键是如何把数字用图表示,如何比较,保存。。一般分为服务器端比较验证,和客户端。服务器端用session,客户端用cookie 上面的代码中好像有一个问题~~就是不能在此页面中换图片,换的话最后一句会抛出IO异常,不知道try-catch之后还会不会出现问题,可以试下 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 main(String[] args) { new JPasswordFieldDemo(); }}这是我编的。你试一试 数据库查询 关于Extjs 页面显示的 大家帮帮看看 根据url获取网页代码 个别字乱码问题 在线等,while (rs.next())问题 java,jsp,jsf技术群26936145 这段代码没有错,但系统总提示有错,请高手指教 请教一个问题:struts action forward??(在线等) 谁能担任一个jsp+jsTree的例子 帮忙看看我这段程序哪里出错,谢谢! 请问Apache JServ是做什么用的? request.getCookies()方法得不到cookie,只得到一个内存cookie. 为什么我用hibernate的反向工程生成不了映射文件我POJO类
参考我们教程中的例子:http://www.family168.com/tutorial/jsp/html/jsp-ch-12.html
<%@ 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());
%>
一般分为服务器端比较验证,和客户端。
服务器端用session,客户端用cookie
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 main(String[] args) {
new JPasswordFieldDemo();
}
}
这是我编的。你试一试