Jsp 驗證碼(檢驗碼)的實現

來源:互聯網
上載者:User
  1. 下載必要檔案
    1)下載Jakarta-Taglibs:
      http://people.apache.org/builds/jakarta-taglibs-sandbox/nightly/
      解壓後取出taglibs-image.jar放於/WEB-INF/lib下
      將taglibs-image.tld拷到/WEB-INF/下

    2)下載Poor Man's Imaging Wrapper:Image Tag 用到了這個包
      http://www.mullassery.com/downloads/ 填寫一些必要資訊後就可以免費下載了
      解壓後將pmiw.jar拷到/WEB-INF/lib下

  2. 在web.xml中配置ImageServlet
      <context-param>
        <param-name>ImageServletMapping</param-name>
        <param-value>/gen-image</param-value>
      </context-param>
      <servlet>
        <servlet-name>ImageServlet</servlet-name>
        <servlet-class>org.apache.taglibs.image.ImageServlet</servlet-class>
      </servlet>
     <servlet-mapping>
        <servlet-name>ImageServlet</servlet-name>
        <url-pattern>/gen-image/*</url-pattern>
      </servlet-mapping>
  3. 在jsp頁面中加入實現代碼(部分內容參考了http://technology.a.lunqun.com/read.php?tid=149894&page=e&fpage=1)

    1)在頁首加入<%@ taglib uri="/WEB-INF/taglibs-image.tld" prefix="img" %>

    2)在頁面頂端加入
    <%
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    %>

    3)在想加上驗證碼的地方加上如下代碼:
    <%
    int num = (int) java.lang.Math.round(java.lang.Math.random() * 8999);
    String sRand = "" + (1000 +num);
    session.setAttribute("userInfo.authcode",sRand);
    %>

    <img:image src="/graphics/auth.jpg" refresh="true">

    <img:text text="<%=sRand.substring(0,1)%>" x="7" y="-1" font="Times New Roman" bold="false" size="22" color="0x993399" italic="true" />
    <img:text text="<%=sRand.substring(1,2)%>" x="17" y="-1" font="Times New Roman" bold="false" size="22" color="0x660033" italic="false" />
    <img:text text="<%=sRand.substring(2,3)%>" x="27" y="-1" font="Times New Roman" bold="true" size="18" color="0xcc3366" italic="true" />
    <img:text text="<%=sRand.substring(3,4)%>" x="37" y="-1" font="Times New Roman" bold="false" size="22" color="0x990099" italic="false" />

    <%if(num<4500){%>
    <img:grayscale brightness="90" />
    <%}%>
    </img:image>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.