Servlet(七)生成验证码
1、ImageCode.java
package com.hunhun.utils; 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.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; /** * Servlet implementation class ImageCode */ @WebServlet("/ImageCode") public class ImageCode extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ImageCode() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //禁止浏览器缓存随机图片 response.setDateHeader("Expires", -1); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache"); //通知浏览器以图片方式显示数据 response.setHeader("Content-Type", "image/png"); //在内存中创建图片 BufferedImage image = new BufferedImage(75,20,BufferedImage.TYPE_INT_RGB); //获取图片上下文 Graphics g = image.getGraphics(); //设置背景色 g.setColor(Color.black); g.fillRect(0, 0, 75, 20); //生成随机类 Random r = new Random(); //生成4位验证码 String srcChars ="abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String number = ""; for(int i=0;i<4;i++){ number +=srcChars.charAt(r.nextInt(srcChars.length())); } //设置字体、颜色 g.setFont(new Font("Arial",Font.BOLD|Font.ITALIC,16));//new Font(name, style, size) g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); // 随机产生15条干扰线。使图象中的认证码不易被其他程序探測到 for (int i = 0; i < 15; i++) { int x = r.nextInt(75); int y = r.nextInt(20); int xl = r.nextInt(12); int yl = r.nextInt(12); g.drawLine(x, y, x + xl, y + yl); } g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); g.drawString(number,10,15); //保存session request.getSession().setAttribute("code",number); OutputStream os = response.getOutputStream(); ImageIO.write(image, "png", os); // JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os); // encoder.encode(image); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doGet(request, response); } }
2.login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <form action="/LittleMall/LoginCServlet" method="post"> <input type="text" name="code" value=""><img alt="code" src="/LittleMall/ImageCode"><br/> <input type="submit" value="登录"> </form> </body> </html>
图片样式: