<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還得開發人員自己寫  回復  更多評論   


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


    網站導航:
     
    主站蜘蛛池模板: 亚洲一区二区三区免费在线观看| 一级成人a免费视频| 91情国产l精品国产亚洲区| 亚洲日韩精品一区二区三区| 久久精品国产精品亚洲人人| 亚洲大尺度无码无码专区| 2020天堂在线亚洲精品专区| 美女黄色免费网站| 成人久久免费网站| 日韩在线播放全免费| 成人免费男女视频网站慢动作| 免费在线观看中文字幕| 亚洲综合久久综合激情久久| 亚洲国产欧洲综合997久久| 黄床大片免费30分钟国产精品| 久久久久高潮毛片免费全部播放| 国产资源免费观看| 亚洲专区在线视频| 成年人网站免费视频| 亚洲va中文字幕无码久久 | 亚洲日韩v无码中文字幕| 中文字幕不卡免费高清视频| 国产精品视_精品国产免费 | 亚洲人JIZZ日本人| 99ee6热久久免费精品6| 亚洲精品tv久久久久| 亚洲精品综合久久中文字幕 | 久久国产乱子伦精品免费看| 俄罗斯极品美女毛片免费播放| 又大又硬又粗又黄的视频免费看| 毛片在线免费视频| 亚洲成人在线免费观看| 日本高清免费观看| 亚洲最大的黄色网| 亚洲国产成人久久综合碰| 激情无码亚洲一区二区三区| 亚洲国产精品免费观看| 亚洲邪恶天堂影院在线观看| a毛片在线还看免费网站| 亚洲精品一级无码鲁丝片 | 日本中文一区二区三区亚洲|