java部分
package com.servlet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class IdentityServlet extends HttpServlet { //不包括0,O,1,I等难以辨认的字符 public static final char[] CHARS={'2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z'}; public static Random random=new Random(); //获取随机码 public static String getRandomString(){ StringBuffer buffer=new StringBuffer(); for(int i=0;i<6;i++){ buffer.append(CHARS[random.nextInt(CHARS.length)]); } return buffer.toString(); } //获取随机颜色 public static Color getRandomColor(){ return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)); } //获取所给颜色的相反值 public static Color getReverseColor(Color c){ return new Color(255-c.getRed(),255-c.getGreen(),255-c.getBlue()); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("image/jpeg");//设置输出类型 String randomString=getRandomString();//获取随机数 request.getSession(true).setAttribute("randomString", randomString);//将随机数放入session中 int width=100;//图片宽度 int height=30;//图片高度 Color color=getRandomColor();//获取颜色,用于背景色 Color reverse=getReverseColor(color);//发色,用于前背景 BufferedImage bi=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);//创建一个彩色图片 Graphics2D g=bi.createGraphics();//获取绘图对象 g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,16));//设置字体 g.setColor(color);//设置颜色 g.fillRect(0, 0, width, height);//绘制背景 g.setColor(reverse); g.drawString(randomString, 18, 20);//设置颜色 //绘制遭点 for(int i=0, n=random.nextInt(50);i<n;i++){ g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1); } ServletOutputStream out=response.getOutputStream(); JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out); encoder.encode(bi); out.flush(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
web.xml部分
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <servlet> <servlet-name>IdentityServlet</servlet-name> <servlet-class>com.servlet.IdentityServlet</servlet-class> </servlet> <servlet-name>IdentityServlet</servlet-name> <url-pattern>/servlet/IdentityServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
html部分
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% 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> <script type="text/javascript"> function reloadImage(){ document.getElementById('btn').disabled=true; document.getElementById('identity').src='servlet/IdentityServlet?ts='+new Date().getTime(); } </script> </head> <body> <img src="servlet/IdentityServlet" id='identity' onclick="btn.disabled=false;"/> <input type="button" value="换一个" onclick="reloadImage()" id='btn'> </body> </html>
运行结果: