我们这里先用注解配置一个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>
后续还会添加刷新的方法