第一步:建一个动态工程check,一个check.java(servlet类),一个login.jsp文件
第二步:编写check.java文件,内容为:
01.package check; 02.import javax.servlet.ServletException; 03.import javax.servlet.http.*; 04.import java.io.*; 05.import java.awt.*; 06.import java.awt.image.*; 07.import java.util.*; 08.import javax.imageio.*; 09. 10./** 11. * @author yeeku.H.lee kongyeeku@163.com 12. * @version 1.0 13. * <br>Copyright (C), 2005-2008, yeeku.H.Lee 14. * <br>This program is protected by copyright laws. 15. * <br>Program Name: 16. * <br>Date: 17. */ 18.public class check extends HttpServlet 19.{ 20. private Font mFont = new Font("Arial Black", Font.PLAIN, 16); 21. public void init() throws ServletException 22. { 23. super.init(); 24. } 25. Color getRandColor(int fc,int bc) 26. { 27. Random random = new Random(); 28. if(fc>255) fc=255; 29. if(bc>255) bc=255; 30. int r=fc+random.nextInt(bc-fc); 31. int g=fc+random.nextInt(bc-fc); 32. int b=fc+random.nextInt(bc-fc); 33. return new Color(r,g,b); 34. } 35. 36. public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 37. { 38. response.setHeader("Pragma","No-cache"); 39. response.setHeader("Cache-Control","no-cache"); 40. response.setDateHeader("Expires", 0); 41. response.setContentType("image/jpeg"); 42. 43. int width=100, height=18; 44. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 45. 46. Graphics g = image.getGraphics(); 47. Random random = new Random(); 48. g.setColor(getRandColor(200,250)); 49. g.fillRect(1, 1, width-1, height-1); 50. g.setColor(new Color(102,102,102)); 51. g.drawRect(0, 0, width-1, height-1); 52. g.setFont(mFont); 53. 54. g.setColor(getRandColor(160,200)); 55. for (int i=0;i<155;i++) 56. { 57. int x = random.nextInt(width - 1); 58. int y = random.nextInt(height - 1); 59. int xl = random.nextInt(6) + 1; 60. int yl = random.nextInt(12) + 1; 61. g.drawLine(x,y,x + xl,y + yl); 62. } 63. for (int i = 0;i < 70;i++) 64. { 65. int x = random.nextInt(width - 1); 66. int y = random.nextInt(height - 1); 67. int xl = random.nextInt(12) + 1; 68. int yl = random.nextInt(6) + 1; 69. g.drawLine(x,y,x - xl,y - yl); 70. } 71. 72. String sRand=""; 73. for (int i=0;i<6;i++) 74. { 75. String tmp = getRandomChar(); 76. sRand += tmp; 77. g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110))); 78. g.drawString(tmp,15*i+10,15); 79. } 80. 81. HttpSession session = request.getSession(true); 82. session.setAttribute("rand",sRand); 83. g.dispose(); 84. ImageIO.write(image, "JPEG", response.getOutputStream()); 85. } 86. private String getRandomChar() 87. { 88. int rand = (int)Math.round(Math.random() * 2); 89. long itmp = 0; 90. char ctmp = '\u0000'; 91. switch (rand) 92. { 93. case 1: 94. itmp = Math.round(Math.random() * 25 + 65); 95. ctmp = (char)itmp; 96. return String.valueOf(ctmp); 97. case 2: 98. itmp = Math.round(Math.random() * 25 + 97); 99. ctmp = (char)itmp; 100. return String.valueOf(ctmp); 101. default : 102. itmp = Math.round(Math.random() * 9); 103. return String.valueOf(itmp); 104. } 105. } 106.}
第三步:编写login.jsp文件,内容为:
01.<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> 02.<% 03.String path = request.getContextPath(); 04.String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 05.%> 06. 07.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 08.<html> 09. <head> 10. <base href="<%=basePath%>"> 11. 12. <title>My JSP 'index.jsp' starting page</title> 13. <meta http-equiv="pragma" content="no-cache"> 14. <meta http-equiv="cache-control" content="no-cache"> 15. <meta http-equiv="expires" content="0"> 16. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17. <meta http-equiv="description" content="This is my page"> 18. <!-- 19. <link rel="stylesheet" type="text/css" href="styles.css"> 20. --> 21. <script> 22. function refresh() 23. { 24. document.getElementById("authImg").src='check?now=' + new Date(); 25. } 26. </script> 27. </head> 28. 29. <body> 30. <% 31. 32. 33. %> 34. 验证码如图:<img src="check" id="authImg"/>看不清?<a href="#" onClick="refresh()">单击此处刷新</a> 35. </body> 36.</html>
第四步:在web.xml文件中配置代码,内容为:
01.<servlet> 02. <servlet-name>img</servlet-name> 03. <servlet-class>check.check</servlet-class> 04. </servlet> 05. 06. <servlet-mapping> 07. <servlet-name>img</servlet-name> 08. <url-pattern>/check</url-pattern> 09. </servlet-mapping>
第五步:在tomcat服务器上发布项目check,运行login.jsp,运行结果为: