![](https://i-blog.csdnimg.cn/blog_migrate/f7a9d00df653f41840b67c77375ecde6.png)
ActiveCodeUtils.java
![](https://i-blog.csdnimg.cn/blog_migrate/7a276e37b8dd78d6d6092eeea9a2b4cd.png)
package cn.itcast.itcaststore.tag;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import cn.itcast.itcaststore.domain.User;
public class PrivilegeTag extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
PageContext context = (PageContext) this.getJspContext();
HttpServletRequest request = (HttpServletRequest) context.getRequest();
HttpServletResponse response = (HttpServletResponse) context.getResponse();
User user = (User) context.getSession().getAttribute("user");
if (user == null) {
response.sendRedirect(request.getContextPath() + "/client/error/privilege.jsp");
}
}
}
ChectImageServlet.java
![](https://i-blog.csdnimg.cn/blog_migrate/1492e034286b9d47c2f0ad3911c4515d.png)
package cn.itcast.itcaststore.utils;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 生成验证码的工具类
*/
public class CheckImageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// 集合中保存所有成语
private List<String> words = new ArrayList<String>();
@Override
public void init() throws ServletException {
// 初始化阶段,读取new_words.txt
// web工程中读取文件,必须使用绝对磁盘路径
String path = getServletContext().getRealPath("/WEB-INF/new_words.txt");
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(path), "utf-8"));
String line;
while ((line = reader.readLine()) != null) {
words.add(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 禁止缓存
// response.setHeader("Cache-Control", "no-cache");
// response.setHeader("Pragma", "no-cache");
// response.setDateHeader("Expires", -1);
int width = 180;
int height = 30;
// 步骤一 绘制一张内存中图片
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 步骤二 图片绘制背景颜色 ---通过绘图对象
Graphics graphics = bufferedImage.getGraphics();// 得到画图对象 --- 画笔
// 绘制任何图形之前 都必须指定一个颜色
graphics.setColor(g