<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    §封Ja葬va§

    自定義圖形驗證碼標簽

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

    以下為詳細定義過程:
    <?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>

        <!--   
        驗證碼控件
        使用說明:
        jsp中使用范例:
        1.無參數:<htdz-tag:CheckCodeTag/>
        2.全部參數:<htdz-tag:CheckCodeTag id="checkCodeImg" height="18" width="58"/>
       
        如果用戶未設置參數,則默認值分別為:height="18" width="58"
        -->
        <description>驗證碼控件</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=\"重新獲取驗證碼\"");
            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個
                                         '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 §朱家二少§ 閱讀(420) 評論(1)  編輯  收藏 所屬分類: J2SE

    Feedback

    # re: 自定義圖形驗證碼標簽 2011-05-23 13:24 tandong

    你這個標簽有什么意義呢 Servlet還得開發人員自己寫  回復  更多評論   


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


    網站導航:
     
    主站蜘蛛池模板: 亚洲中文字幕久久无码| 亚洲自偷自拍另类12p| 国产亚洲精彩视频| 永久免费AV无码网站在线观看| 亚洲日本乱码卡2卡3卡新区| 四虎www成人影院免费观看| 亚洲av日韩综合一区久热| 国产美女无遮挡免费网站| 国产午夜亚洲精品不卡电影| 亚洲午夜无码AV毛片久久| 中文字幕视频免费在线观看| 亚洲成A∨人片在线观看不卡| 全部免费毛片在线播放| 亚洲免费福利视频| 日本免费观看网站| 国产激情久久久久影院老熟女免费 | 亚洲性无码AV中文字幕| 日韩免费视频网站| 亚洲国产免费综合| 亚洲男人天堂av| 免费观看的毛片手机视频| 一级特级女人18毛片免费视频| 亚洲国产AV无码专区亚洲AV| 在线看片免费人成视久网| 亚洲欧美中文日韩视频| 亚洲一级黄色视频| 亚洲精品视频免费看| 亚洲A∨精品一区二区三区下载| 亚洲综合国产精品第一页 | 无码专区永久免费AV网站| 苍井空亚洲精品AA片在线播放| 亚洲午夜久久久久久久久电影网| 久久久久久夜精品精品免费啦| 在线观看亚洲AV日韩AV| 亚洲真人日本在线| 国产一卡2卡3卡4卡2021免费观看 国产一卡2卡3卡4卡无卡免费视频 | 久久亚洲精品人成综合网| 成人免费一区二区三区在线观看| 亚洲精品视频免费观看| 亚洲欧洲另类春色校园小说| avtt亚洲天堂|