关于在Java生成验证码的实例

  我们这里先用注解配置一个servlet 

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;


import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(name="validateCode",value="/validateCode")
public class ValidateCode extends HttpServlet {


public ValidateCode() {
super();
}



public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


response.setContentType("text/html");
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


response.setContentType("text/html");
}


public void init() throws ServletException {

}
 @Override
protected void service(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
     response.setHeader("Content-Type","image/jpeg;charset=UTF-8");
     response.setHeader("Pragma", "no-cache");
     response.setHeader("Cache-Control", "no-cache");
     //表示这个内容不设置为缓存
     response.setDateHeader("Expires", -1);
    //获得outputstream 
     OutputStream outputStream=response.getOutputStream();
     BufferedImage image=new BufferedImage(80, 35, BufferedImage.TYPE_INT_RGB);
    //获得画笔
     Graphics g= image.getGraphics();
     //获取画笔的颜色
     Color color =g.getColor();
   //画一个矩形
     g.fillRect(0, 0, 80, 35);
     char[] ch="abcdefghjkmnopqrstuvwxyz0234567891ABCDEFGHJKMNOPQRSTUVWXYZ".toCharArray();
     int length=ch.length;
     Random random=new Random();
     Font font[]=new Font[5];
     font[0]=new Font("Ravie", Font.PLAIN, 24);
     font[1]=new Font("Autique Olive Compact", Font.PLAIN, 24);
     font[2]=new Font("Forte", Font.PLAIN, 24);
     font[3]=new Font("Wide Latin", Font.PLAIN, 24);
     font[4]=new Font("Gill Sans Ultra Bold", Font.PLAIN, 24);
         //随机获得一个字体
     Font nowFont=font[random.nextInt(5)];
     for(int i=0;i<4;i++){
                 //设置字体的样式
    g.setFont(nowFont);
    String rand =new Character(ch[random.nextInt(length)]).toString();
    g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
           g.drawString(rand, 20*i+6, 25);     
     }
     //封装干扰线
     for(int i=0;i<80;i++){
    int x1=random.nextInt(80);
    int y1=random.nextInt(35);
    g.drawOval(x1, y1, 2, 2);
     }
     ImageIO.write(image, "JPEG", outputStream);
 }
}


在index.jsp之中调用这个servlet,就可以生成我们想要的验证码了

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
   <img src="validateCode">
  </body>
</html>


后续还会添加刷新的方法

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值