§封Ja葬va§

          自定義圖形驗(yàn)證碼標(biāo)簽

          JSP頁面上引入:
          <%@ taglib uri="/htdz-tag" prefix="htdz-tag"%>
          使用自定義圖形驗(yàn)證碼標(biāo)簽自動生成驗(yàn)證碼:
          <htdz-tag:CheckCodeTag />

          以下為詳細(xì)定義過程:
          <?xml version="1.0" encoding="UTF-8" ?>
          <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
           version="2.0">
              <description>htdz tag</description>
              <display-name>htdz tag</display-name>
              <tlib-version>1.0</tlib-version>
              <short-name>htdz-tag</short-name>
              <uri>/htdz-tag</uri>

              <tag>

              <!--   
              驗(yàn)證碼控件
              使用說明:
              jsp中使用范例:
              1.無參數(shù):<htdz-tag:CheckCodeTag/>
              2.全部參數(shù):<htdz-tag:CheckCodeTag id="checkCodeImg" height="18" width="58"/>
             
              如果用戶未設(shè)置參數(shù),則默認(rèn)值分別為:height="18" width="58"
              -->
              <description>驗(yàn)證碼控件</description>
              <name>CheckCodeTag</name>
              <tag-class>com.htdz.util.tag.CheckCodeTag</tag-class>
              <body-content>JSP</body-content>
              <attribute>
                  <description>id</description>
                  <name>id</name>
                  <required>false</required>
                  <rtexprvalue>true</rtexprvalue>
              </attribute>
              <attribute>
                  <description>高</description>
                  <name>height</name>
                  <required>false</required>
                  <rtexprvalue>true</rtexprvalue>
              </attribute>
              <attribute>
                  <description>寬</description>
                  <name>width</name>
                  <required>false</required>
                  <rtexprvalue>true</rtexprvalue>
              </attribute>

              </tag>
          <taglib/>

          CheckCodeTag.java:

          public class CheckCodeTag extends TagSupport {
              private String id;
              private String height;
              private String width;

              public CheckCodeTag() {
              }

              @SuppressWarnings( { "unchecked", "static-access" })
              public int doStartTag() throws JspException {
                  StringBuffer html = new StringBuffer();
                  if (height == null || height.length() == 0)
                      height = "18";
                  if (width == null || width.length() == 0)
                      width = "60";
                  html.append("<img alt=\"重新獲取驗(yàn)證碼\"");
                  if (id != null && id.length() > 0) {
                      html.append(" id=\"");
                      html.append(id);
                      html.append("\"");
                  }
                  html.append(" height=\"");
                  html.append(height);
                  html.append("\" width=\"");
                  html.append(width);
                  html.append("\" src=\"/checkCodeImg\" onclick=\"this.src='/checkCodeImg?now='+new Date();\" style=\"cursor: pointer\" />");
                  try {
                      pageContext.getOut().println(html.toString());
                  } catch (Exception e) {
                      throw new JspException(e.getMessage());
                  }
                  return this.SKIP_BODY;
              }

              public void setId(String id) {
                  this.id = id;
              }

              public void setHeight(String height) {
                  this.height = height;
              }

              public void setWidth(String width) {
                  this.width = width;
              }
          }



          web.xml:
              <servlet>
                  <servlet-name>img</servlet-name>
                  <servlet-class>com.htdz.util.CheckCodeImg</servlet-class>
              </servlet>
              <servlet-mapping>
                  <servlet-name>img</servlet-name>
                  <url-pattern>/checkCodeImg</url-pattern>
              </servlet-mapping>


          CheckCodeImg.java:

          public class CheckCodeImg extends HttpServlet {
              private Font mFont = new Font("Arial black", Font.PLAIN, 16);

              public void init() throws ServletException {
                  super.init();
              }

              Color getRandColor(int fc, int bc) {
                  Random random = new Random();
                  if (fc > 255)
                      fc = 255;
                  if (bc > 255)
                      bc = 255;
                  int r = fc + random.nextInt(bc - fc);
                  int g = fc + random.nextInt(bc - fc);
                  int b = fc + random.nextInt(bc - fc);
                  return new Color(r, g, b);
              }

              public void service(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException {
                  response.setHeader("Pragma", "No-cache");
                  response.setHeader("Cache-Control", "no-cache");
                  response.setDateHeader("Expires", 0);
                  response.setContentType("image/png");

                  int width = 60, height = 18;
                  BufferedImage image = new BufferedImage(width, height,
                  BufferedImage.TYPE_INT_RGB);

                  Graphics g = image.getGraphics();
                  Random random = new Random();
                  g.setColor(getRandColor(200, 250));
                  g.fillRect(1, 1, width - 1, height - 1);
                  g.setColor(new Color(102, 102, 102));
                  g.drawRect(0, 0, width - 1, height - 1);
                  g.setFont(mFont);

                  g.setColor(getRandColor(160, 200));
                  for (int i = 0; i < 155; i++) {
                      int x = random.nextInt(width - 1);
                      int y = random.nextInt(height - 1);
                      int xl = random.nextInt(6) + 1;
                      int yl = random.nextInt(12) + 1;
                      g.drawLine(x, y, x + xl, y + yl);
                  }
                  for (int i = 0; i < 70; i++) {
                      int x = random.nextInt(width - 1);
                      int y = random.nextInt(height - 1);
                      int xl = random.nextInt(12) + 1;
                      int yl = random.nextInt(6) + 1;
                      g.drawLine(x, y, x - xl, y - yl);
                  }

                  char[] codes = { // 'a','b','c','d','e','f','h','k','m','n','r','s','t','x','y',//15個(gè)
                                               '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
                                           // 'A','B','C','D','E','F','G','H','K','N','S','T','X','Y'//14
                  };
                  String sRand = "";
                  for (int i = 0; i < 4; i++) {
                      int j = random.nextInt(10);
                      String tmp = String.valueOf(codes[j]);
                      sRand += tmp;
                      g.setColor(new Color(20 + random.nextInt(110), 20 + random
                      .nextInt(110), 20 + random.nextInt(110)));
                      g.drawString(tmp, 15 * i + 2, 15);
                  }

                  HttpSession session = request.getSession(true);
                  session.setAttribute(MyConstant.SESSION_CHECKCODE, sRand);
                  g.dispose();
                  ImageIO.write(image, "PNG", response.getOutputStream());
             }
          }

          posted on 2009-04-04 00:52 §朱家二少§ 閱讀(422) 評論(1)  編輯  收藏 所屬分類: J2SE

          Feedback

          # re: 自定義圖形驗(yàn)證碼標(biāo)簽 2011-05-23 13:24 tandong

          你這個(gè)標(biāo)簽有什么意義呢 Servlet還得開發(fā)人員自己寫  回復(fù)  更多評論   


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 阳江市| 施甸县| 额敏县| 东台市| 宁津县| 安龙县| 财经| 华阴市| 历史| 新巴尔虎右旗| 台南市| 天气| 毕节市| 乌鲁木齐市| 邯郸县| 封开县| 资溪县| 泊头市| 平凉市| 通许县| 巨鹿县| 宝山区| 金昌市| 营口市| 裕民县| 合作市| 太和县| 沐川县| 建水县| 城步| 太湖县| 旌德县| 繁昌县| 苍梧县| 扶绥县| 十堰市| 海宁市| 泸水县| 万载县| 调兵山市| 沾益县|