1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %> 2 3 <% 4 String action = request.getParameter("action"); 5 String safecodeText = request.getParameter("safecodeTest"); 6 if("action".equals(action)){ 7 String safecode = (String)session.getAttribute("safecode"); 8 if(safecode.equals(safecodeText)){ 9 out.print("验证码正确!"); 10 }else{ 11 out.print("验证码错误!请重新输入!"); 12 } 13 } 14 %> 15 <html> 16 <head> 17 <title>验证码测试</title> 18 </head> 19 20 <body> 21 <form action="servlet/demo" method="post"> 22 <input type="hidden" name="action" value="action"/> 23 <img alt="验证码" src="servlet/demo"> 24 <input type="text" name="safecodeTest"> 25 <input type="submit" value="go"> 26 </form> 27 </body> 28 </html>
1 import java.io.*; 2 3 import javax.servlet.*; 4 import javax.servlet.http.*; 5 import java.util.Random; 6 import java.awt.*; 7 import java.awt.image.*; 8 import javax.imageio.*; 9 10 11 public class demo extends HttpServlet { 12 //产生随即的字体 13 private Font getFont() { 14 Random random = new Random(); 15 Font font[] = new Font[5]; 16 font[0] = new Font("Ravie", Font.PLAIN, 24); 17 font[1] = new Font("Antique Olive Compact", Font.PLAIN, 24); 18 font[2] = new Font("Forte", Font.PLAIN, 24); 19 font[3] = new Font("Wide Latin", Font.PLAIN, 24); 20 font[4] = new Font("Gill Sans Ultra Bold", Font.PLAIN, 24); 21 return font[random.nextInt(5)]; 22 } 23 24 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 25 throws ServletException, IOException { 26 // 设置响应头 Content-type类型 27 resp.setContentType("image/jpeg"); 28 // 以下三句是用于设置页面不缓存 29 resp.setHeader("Pragma", "No-cache"); 30 resp.setHeader("Cache-Control", "No-cache"); 31 resp.setDateHeader("Expires", 0); 32 33 OutputStream os = resp.getOutputStream(); 34 int width = 83, height = 30; 35 // 建立指定宽、高和BufferedImage对象 36 BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); 37 38 Graphics g = image.getGraphics(); // 该画笔画在image上 39 Color c = g.getColor(); // 保存当前画笔的颜色,用完画笔后要回复现场 40 g.fillRect(0, 0, width, height); 41 42 43 char[] ch = "abcdefghjkmnpqrstuvwxyz23456789".toCharArray(); // 随即产生的字符串 不包括 i l(小写L) o(小写O) 1(数字1)0(数字0) 44 int length = ch.length; // 随即字符串的长度 45 String sRand = ""; // 保存随即产生的字符串 46 Random random = new Random(); 47 for (int i = 0; i < 4; i++) { 48 // 设置字体 49 g.setFont(getFont()); 50 // 随即生成0-9的数字 51 String rand = new Character(ch[random.nextInt(length)]).toString(); 52 sRand += rand; 53 // 设置随机颜色 54 g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255))); 55 g.drawString(rand, 20 * i + 6, 25); 56 } 57 //产生随即干扰点 58 for (int i = 0; i < 20; i++) { 59 int x1 = random.nextInt(width); 60 int y1 = random.nextInt(height); 61 g.drawOval(x1, y1, 2, 2); 62 } 63 g.setColor(c); // 将画笔的颜色再设置回去 64 g.dispose(); 65 66 //将验证码记录到session 67 req.getSession().setAttribute("safecode", sRand); 68 // 输出图像到页面 69 ImageIO.write(image, "JPEG", os); 70 71 } 72 73 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 74 throws ServletException, IOException { 75 doGet(req, resp); 76 } 77 78 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>JavaWebDemo</display-name> 4 <servlet> 5 <description>This is the description of my J2EE component</description> 6 <display-name>This is the display name of my J2EE component</display-name> 7 <servlet-name>demo</servlet-name> 8 <servlet-class>demo</servlet-class> 9 </servlet> 10 11 <servlet-mapping> 12 <servlet-name>demo</servlet-name> 13 <url-pattern>/servlet/demo</url-pattern> 14 </servlet-mapping> 15 16 <welcome-file-list> 17 <welcome-file>index.html</welcome-file> 18 <welcome-file>index.htm</welcome-file> 19 <welcome-file>index.jsp</welcome-file> 20 <welcome-file>default.html</welcome-file> 21 <welcome-file>default.htm</welcome-file> 22 <welcome-file>default.jsp</welcome-file> 23 </welcome-file-list> 24 </web-app>
1 package action; 2 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.Graphics; 6 import java.awt.image.BufferedImage; 7 import java.io.ByteArrayInputStream; 8 import java.io.ByteArrayOutputStream; 9 import java.util.Map; 10 11 import javax.imageio.ImageIO; 12 import javax.imageio.stream.ImageOutputStream; 13 14 import com.opensymphony.xwork2.ActionContext; 15 import com.opensymphony.xwork2.ActionSupport; 16 17 public class ImageAction extends ActionSupport { 18 19 /** 20 * 21 */ 22 private static final long serialVersionUID = 1L; 23 private ByteArrayInputStream inputStream; 24 public String createRandomString(){ 25 String str=""; 26 for(int i=0;i<4;i++){ 27 str+=Integer.toString((new Double(Math.random()*10)).intValue()); 28 } 29 return str; 30 } 31 public Color createRandomColor(){ 32 int r=(new Double(Math.random()*256)).intValue(); 33 int g=(new Double(Math.random()*256)).intValue(); 34 int b=(new Double(Math.random()*256)).intValue(); 35 return new Color(r,g,b); 36 } 37 public BufferedImage createImage(String str){ 38 int width=60; 39 int height=22; 40 BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); 41 Graphics g=image.getGraphics(); 42 g.setColor(Color.WHITE); 43 g.fillRect(0, 0, width, height); 44 g.setColor(Color.BLACK); 45 g.drawRect(0, 0, width-1, height-1); 46 g.setFont(new Font("Atlantic Inline",Font.PLAIN,18)); 47 g.setColor(this.createRandomColor()); 48 g.drawString(Character.toString(str.charAt(0)), 8, 17); 49 g.drawString(Character.toString(str.charAt(1)), 20, 17); 50 g.drawString(Character.toString(str.charAt(2)), 33, 17); 51 g.drawString(Character.toString(str.charAt(3)), 45, 17); 52 g.dispose(); 53 return image; 54 } 55 public ByteArrayInputStream createInputStream() throws Exception{ 56 String str=this.createRandomString(); 57 BufferedImage image=this.createImage(str); 58 ActionContext actioncontext=ActionContext.getContext(); 59 Map session=actioncontext.getSession(); 60 session.put("random", str); 61 ByteArrayOutputStream output=new ByteArrayOutputStream(); 62 ImageOutputStream imageout=ImageIO.createImageOutputStream(output); 63 ImageIO.write(image, "JPEG", imageout); 64 imageout.close(); 65 ByteArrayInputStream input=new ByteArrayInputStream(output.toByteArray()); 66 output.close(); 67 return input; 68 } 69 public String execute() throws Exception{ 70 setInputStream(createInputStream()); 71 return SUCCESS; 72 } 73 public ByteArrayInputStream getInputStream(){ 74 return inputStream; 75 } 76 public void setInputStream(ByteArrayInputStream inputStream){ 77 this.inputStream=inputStream; 78 } 79 }