1.ImageServlet类的实现:

 

  1. package com.mobile.control;  
  2.   
  3.    
  4.   
  5. import java.awt.Color;  
  6.   
  7. import java.awt.Font;  
  8.   
  9. import java.awt.Graphics;  
  10.   
  11. import java.awt.p_w_picpath.BufferedImage;  
  12.   
  13. import java.io.IOException;  
  14.   
  15. import java.util.Random;  
  16.   
  17.    
  18.   
  19. import javax.p_w_picpathio.ImageIO;  
  20.   
  21. import javax.servlet.ServletException;  
  22.   
  23. import javax.servlet.http.HttpServlet;  
  24.   
  25. import javax.servlet.http.HttpServletRequest;  
  26.   
  27. import javax.servlet.http.HttpServletResponse;  
  28.   
  29.    
  30.   
  31. public class ImageServlet extends HttpServlet {  
  32.   
  33.    
  34.   
  35.        public void doGet(HttpServletRequest request, HttpServletResponse response)  
  36.   
  37.                      throws ServletException, IOException {  
  38.   
  39.    
  40.   
  41.               this.doPost(request, response);  
  42.   
  43.        }  
  44.   
  45.    
  46.   
  47.        // 生成数字和字母的验证码  
  48.   
  49.        public void doPost(HttpServletRequest request, HttpServletResponse response)  
  50.   
  51.                      throws ServletException, IOException {  
  52.   
  53.    
  54.   
  55.               BufferedImage img = new BufferedImage(6822,  
  56.   
  57.                             BufferedImage.TYPE_INT_RGB);  
  58.   
  59.               // 得到该图片的绘图对象  
  60.   
  61.               Graphics g = img.getGraphics();  
  62.   
  63.               Random r = new Random();  
  64.   
  65.               Color c = new Color(200150255);  
  66.   
  67.               g.setColor(c);  
  68.   
  69.               // 填充整个图片的颜色  
  70.   
  71.               g.fillRect(006822);  
  72.   
  73.               // 向图片中输出数字和字母  
  74.   
  75.               StringBuffer sb = new StringBuffer();  
  76.   
  77.               char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();  
  78.   
  79.               int index, len = ch.length;  
  80.   
  81.               for (int i = 0; i < 4; i ++) {  
  82.   
  83.                      index = r.nextInt(len);  
  84.   
  85.                      g.setColor(new Color(r.nextInt(88), r.nextInt(188), r.nextInt  
  86.   
  87.    
  88.   
  89. (255)));  
  90.   
  91.                      g.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 22));// 输出的  
  92.   
  93.    
  94.   
  95. 字体和大小                    
  96.   
  97.                      g.drawString("" + ch[index], (i * 15) + 318);//写什么数字,在图片  
  98.   
  99.    
  100.   
  101. 的什么位置画  
  102.   
  103.                      sb.append(ch[index]);  
  104.   
  105.               }  
  106.   
  107.               request.getSession().setAttribute("piccode", sb.toString());  
  108.   
  109.               ImageIO.write(img, "JPG", response.getOutputStream());  
  110.   
  111.    
  112.   
  113.        }       
  114.   
  115. }  


 

2.web.xml中的配置

 

  1. <servlet>  
  2.   
  3.        <servlet-name>ImageServlet</servlet-name>  
  4.   
  5.        <servlet-class>com.mobile.control.ImageServlet</servlet-class>  
  6.   
  7. </servlet>  
  8.   
  9.   
  10.   
  11.   
  12.   
  13. <servlet-mapping>  
  14.   
  15.        <servlet-name>ImageServlet</servlet-name>  
  16.   
  17.        <url-pattern>/servlet/ImageServlet</url-pattern>  
  18.   
  19. </servlet-mapping>  

 

3.页面源程序

 

  1. <%@ page contentType="text/html; charset=utf-8"%>  
  2.   
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  4.   
  5. <html>  
  6.   
  7.        <head>  
  8.   
  9.    
  10.   
  11.               <title>系统登录</title>  
  12.   
  13.    
  14.   
  15.               <meta http-equiv="pragma" content="no-cache">  
  16.   
  17.               <meta http-equiv="cache-control" content="no-cache">  
  18.   
  19.               <meta http-equiv="expires" content="0">  
  20.   
  21.               <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  22.   
  23.               <meta http-equiv="description" content="This is my page">               
  24.   
  25.                 
  26.   
  27.               <link href="<%=request.getContextPath()%>/jsp/style/stylet.css"  
  28.   
  29.                      rel="stylesheet" type="text/css">     
  30.   
  31.               <mce:script type="text/javascript"><!--  
  32.   
  33.                      // 重载验证码  
  34.   
  35.                      function reloadVerifyCode(){  
  36.   
  37.                             var timenow = new Date().getTime();                          
  38.   
  39.                             document.getElementById("safecode").src="<%  
  40.   
  41.    
  42.   
  43. =request.getContextPath()%>/servlet/ImageServlet?d="+timenow;  
  44.   
  45.                      }  
  46.   
  47.                 
  48.   
  49.                 
  50.   
  51. // --></mce:script>  
  52.   
  53.        </head>  
  54.   
  55.          
  56.   
  57.    
  58.   
  59.        <body>  
  60.   
  61.               <div id="box1">  
  62.   
  63.                      <div id="tab_box">  
  64.   
  65.                             <form action="<%=request.getContextPath() %  
  66.   
  67.    
  68.   
  69. >/servlet/LoginServlet" method="post" name="login">  
  70.   
  71.                                    <span class="denglu_font_title">欢迎登录</span>  
  72.   
  73.                                    <br />  
  74.   
  75.                                    <br />  
  76.   
  77.                                    <label for="userid" class="yonghuming_font">  
  78.   
  79.                                           用户:  
  80.   
  81.                                    </label>  
  82.   
  83.                                    <input name="userid" type="text" id="userid"   
  84.   
  85.    
  86.   
  87. accesskey="n"  
  88.   
  89.                                           tabindex="1"  
  90.   
  91.                                           style="font-family: '宋体'; font-size: 12px;   
  92.   
  93.    
  94.   
  95. color: #575757" />  
  96.   
  97.                                    <br />  
  98.   
  99.                                    <br />  
  100.   
  101.                                    <label for="passwd" class="yonghuming_font">  
  102.   
  103.                                           密码:  
  104.   
  105.                                    </label>  
  106.   
  107.                                    <input name="passwd" type="password" id="passwd"   
  108.   
  109.    
  110.   
  111. tabindex="2"  
  112.   
  113.                                           style="font-family: '宋体'; font-size: 12px;   
  114.   
  115.    
  116.   
  117. color: #575757" />  
  118.   
  119.                                    <br />  
  120.   
  121.                                    <br />                                                 
  122.   
  123.                                    <label for="verifyCode" class="yonghuming_font">  
  124.   
  125.                                           验证码:  
  126.   
  127.                                    </label>  
  128.   
  129.                                    <input type="text" id="verifyCode" name="verifyCode"   
  130.   
  131.    
  132.   
  133. size="6" />  
  134.   
  135.                                    <img alt="验证码" id="safecode" src="<%  
  136.   
  137.    
  138.   
  139. =request.getContextPath()%>/servlet/ImageServlet">  
  140.   
  141.                                    <a href="javascript:reloadVerifyCode();"   
  142.   
  143.    
  144.   
  145. mce_href="javascript:reloadVerifyCode();">看不清楚</a>  
  146.   
  147.                                    <input name="denglu_tijiao" type="submit"   
  148.   
  149.    
  150.   
  151. id="denglu_tijiao"  
  152.   
  153.                                           tabindex="3" value="确认" />  
  154.   
  155.                                    <input name="denglu_chongzi" type="reset"   
  156.   
  157.    
  158.   
  159. id="denglu_reset"  
  160.   
  161.                                           tabindex="4" value="重置" />  
  162.   
  163.                                    <input type="hidden" name="flag" value="logon">  
  164.   
  165.                             </form>  
  166.   
  167.                             <font color='red'></font>  
  168.   
  169.                      </div>  
  170.   
  171.               </div>  
  172.   
  173.        </body>  
  174.   
  175. </html>