📄 codemakerservlet.java
字号:
// Fig. 5.6_01: CodeMakerServlet.java
// 生成验证码的Servlet
package com.fatcat.webchart;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.awt.image.*;
import java.awt.*;
import javax.imageio.*;
public class CodeMakerServlet extends HttpServlet
{
private Font[] codeFont =
{
new Font("Algerian", Font.BOLD, 65),
new Font("Vivaldi", Font.BOLD, 85),
new Font("Broadway", Font.BOLD, 60),
new Font("Forte", Font.BOLD, 75)
};
private Color[] color =
{
Color.BLACK, Color.RED, Color.DARK_GRAY, Color.BLUE
};
String codeNumbers = "";
int width = 250, height = 70;
// 处理 HTTP get 请求
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// 清空缓冲区
response.reset();
// 注意这里的MIME类型
response.setContentType("image/png");
// 设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 创建一个 250X70 的图像
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 得到图形环境对象 g
Graphics g = image.getGraphics();
// 填充背景
g.setColor(Color.YELLOW);
g.fillRect(0, 0, width, height);
for (int i = 0; i < 4; i++)
{
drawCode(g, i);
}
drawNoise(g, 30);
// 绘制边框
g.setColor(Color.black);
g.drawRect(0, 0, width - 1, height - 1);
// 将验证码内容保存进session中
HttpSession session = request.getSession(true);
session.setAttribute("codeNumbers", codeNumbers);
// 重设字符串
codeNumbers = "";
// 利用ImageIO类的write方法对图像进行编码
ServletOutputStream sos = response.getOutputStream();
ImageIO.write(image, "PNG", sos);
sos.close();
}
// 绘制验证码
public void drawCode(Graphics graphics, int i)
{
int number = (int)(Math.random() * 10);
graphics.setFont(codeFont[i]);
graphics.setColor(color[i]);
graphics.drawString("" + number, 10 + i * 60, 60);
codeNumbers += number;
}
// 绘制干扰线
public void drawNoise(Graphics graphics, int lineNumber)
{
graphics.setColor(Color.YELLOW);
for (int i = 0; i < lineNumber; i++)
{
int pointX1 = 1 + (int)(Math.random() * width);
int pointY1 = 1 + (int)(Math.random() * height);
int pointX2 = 1 + (int)(Math.random() * width);
int pointY2 = 1 + (int)(Math.random() * height);
graphics.drawLine(pointX1, pointY1, pointX2, pointY2);
}
}
// 处理 HTTP post 请求, 和doGet一样
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}
/**************************************************************************
* (C) Copyright 2004-2005 by Jingkui Zhong(钟京馗) and Huan Tang(唐桓). *
* All Rights Reserved. *
* *
* DISCLAIMER: The authors of this code have used their *
* best efforts in preparing the code. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these codes. The authors *
* shall not be liable in any event for incidental or consequential *
* damages in connection with, or arising out of, the furnishing, *
* performance, or use of these programs. *
**************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -