通过下面这个方法可以得到随机验证码:
public String getIdentifyCode(){//得到验证码(数字+大小写字母)
String str="";
Random rand=new Random();
for(int i=0;i<6;i++){
switch(rand.nextInt(3)){
case 0:int a=(int) (Math.random()*26+65);char b=(char) a;str+=b;break;
case 1:int c=(int) (Math.random()*26+97);char e=(char)c;str+=e;break;
case 2:int d=(int) (Math.random()*10+48);char f=(char)d;str+=f;break;
}
}
return str;
}
下面开始介绍另一种:
在项目中的登录页面写图片验证码:
下面的这个VerifyCode类你可以将它封装成jar包,以后如果要写图片验证码的话,就可以直接使用了。
package cn.itcast.utils;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
public class VerifyCode {
private int w = 70;
private int h = 35;
private Random r = new Random();
private String[] fontNames = {"宋体", "华文楷体", "黑体", "华文新魏", "华文隶书", "微软雅黑", "楷体_GB2312"};
private String codes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private Color bgColor = new Color(240, 240, 240);
private String text ;
private Color randomColor () {//产生随机颜色
int red = r.nextInt(256);
int green = r.nextInt(256);
int blue = r.nextInt(256);
return new Color(red, green, blue);
}
private Font randomFont () {//产生随机字体
int index = r.nextInt(fontNames.length);
String fontName = fontNames[index];
int style = r.nextInt(4);
int size = r.nextInt(5) + 24;
return new Font(fontName, style, size);
}
private void drawLine (BufferedImage image) {//在图片上画干扰线。(一般来说,验证码图片上都会有一、两条干扰线就是为了防止黑客写程序恶意进行验证)
int num = 5;
Graphics2D g2 = (Graphics2D)image.getGraphics();//通过这个类画线
for(int i = 0; i < num; i++) {
int x1 = r.nextInt(w);
int y1 = r.nextInt(h);
int x2 = r.nextInt(w);
int y2 = r.nextInt(h);
g2.setStroke(new BasicStroke(1.5F));
g2.setColor(Color.BLUE);
g2.drawLine(x1, y1, x2, y2);
}
}
private char randomChar () {//产生随机字符
int index = r.nextInt(codes.length());
return codes.charAt(index);
}
private BufferedImage createImage () {//定义一个图像缓冲区来存放你所创建的图片
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)image.getGraphics();
g2.setColor(this.bgColor);
g2.fillRect(0, 0, w, h);
return image;
}
public BufferedImage getImage () {//得到图片
BufferedImage image = createImage();
Graphics2D g2 = (Graphics2D)image.getGraphics();
StringBuilder sb = new StringBuilder();
// 向验证码图片中画4个字符
for(int i = 0; i < 4; i++) {
String s = randomChar() + "";
sb.append(s);
float x = i * 1.0F * w / 4;
g2.setFont(randomFont());
g2.setColor(randomColor());
g2.drawString(s, x, h);
}
this.text = sb.toString();
drawLine(image);
return image;
}
public String getText () {//得到图片上的验证码文本
return text;
}
public static void output (BufferedImage image, OutputStream out)
throws IOException {
ImageIO.write(image, "JPEG", out);//将图片以IO流的方式输出
}
}
具体用法:
当要用的时候,就将上面的类封装成jar包导入你的项目。或者将上面代码写到你的项目中的utils包中。
然后在前端登录页面login.jsp这样写:
上面的
然后,VerifycodeServlet类中这样写:如下
public class VerifycodeServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
res.setContentType("text/html;charset=utf-8");
System.out.println("=========================");
String namevalue=req.getParameter("Vname");//获取参数name的参数值即:logincodeֵ
VerifyCode vc=new VerifyCode();//创建自定义的验证码类的对象
BufferedImage image=vc.getImage();//设置图片缓存器
req.getSession().setAttribute(namevalue, vc.getText());//把图片上的验证码保存到namevalue这个域属性中(即:保存到logincode中),在LoginServlet类中对用户输入的进行验证码进行校对的时候 方便取出来
System.out.println(vc.getText());//在控制台上显示验证码,方便你对照
VerifyCode.output(image,res.getOutputStream());
}
}