欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jsp引用servlet生成的验证码代码演示

程序员文章站 2022-10-10 19:26:15
此演示代码主要包括以下三部分:1.checkcode.java:用于生成验证码2.checkcodeservler3.check.jsp 验证下面是checkco...

此演示代码主要包括以下三部分:
1.checkcode.java:用于生成验证码
2.checkcodeservler
3.check.jsp 验证

下面是checkcode.java的内容:

复制代码 代码如下:

//用于获取四位随机数
     private char maptable[] = {'0','1','2','3','4','5','6','7','8','9'};

     //生成验证码,并返回随机生成的数字
     public string getensure(int width, int height, outputstream os){
         if (width <= 0)
             width = 60;
         if (height <= 0)
             height = 20;

         bufferedimage image = new bufferedimage(width, height,bufferedimage.type_int_rgb);

         // 获取图形上下文
         graphics g = image.getgraphics();

         // 设定背景色
         g.setcolor(new color(0xdccccc));
         g.fillrect(0, 0, width, height);

         // 画边框
         g.setcolor(color.black);
         g.drawrect(0, 0, width - 1, height - 1);

         // 取随机产生的认证码
         string strensure = "";

         // 4代表4位验证码
         for (int i = 0; i < 4; ++i){
             strensure += maptable[(int) (maptable.length * math.random())];
         }

         // 将认证码显示到图象中
         g.setcolor(color.red);
         g.setfont(new font("atlantic inline", font.plain, 14));

         // 画的具体坐标
         string str = strensure.substring(0, 1);
         g.drawstring(str, 8, 14);
         str = strensure.substring(1, 2);
         g.drawstring(str, 20, 15);
         str = strensure.substring(2, 3);
         g.drawstring(str, 35, 18);
         str = strensure.substring(3, 4);
         g.drawstring(str, 45, 15);

         // 释放图形上下文
         g.dispose();

         try{
             // 输出图象到页面
             imageio.write(image, "jpeg", os);
         } catch (ioexception e){
             return "";
         }

         return strensure;          //返回生成的随机数
     }

再是checkcodeservlet的内容

复制代码 代码如下:

public void doget(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception {
        dopost(request, response);
    }

    public void dopost(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception {
        //禁用缓存,每次访问此页面,都重新生成
        response.setheader("pragma","no-cache");
        response.setheader("cache-control","no-cache");
        response.setdateheader("expires", 0);

        //生成验证码的实例对象
        checkcode ie = new checkcode();

        //调用里面的方法,返回的是生成的验证码中的字符串
        string str = ie.getensure(0,0,response.getoutputstream());

        //获得session,并把字符串保存在session中,为后面的对比做基础
        httpsession session = request.getsession();
        session.setattribute("strensure", str);     

    }

然后是web.xml对servlet的配置

复制代码 代码如下:

<servlet>
     <servlet-name>checkservlet</servlet-name>
     <servlet-class>com.blog.servlet.checkservlet</servlet-class>
 </servlet>
<servlet-mapping>   
    <servlet-name>checkservlet</servlet-name>   
    <url-pattern>/check</url-pattern>
 </servlet-mapping>

最后是jsp页面的引用

复制代码 代码如下:

<html>
  <head>
    <title>验证码</title>
    <script type="text/javascript" language="javascript">
    //重新获取验证字符
    function changeimage()
    {
    //单击触发图片重载事件,完成图片验证码的更换
        document.getelementbyid("imgrandom").src = document.getelementbyid("imgrandom").src + '?';
    }
</script>

  </head>

  <body>
        <img alt= "看不清楚?点击更换验证码 " src= "check"   width= "100"   height= "50" id="imgrandom" onclick="changeimage()"/>  
          <a href="javascript:changeimage();">看不清?</a>
  </body>
</html>

在jsp页面中,只需要将img的src的属性指向生成验证码的servlet就可以了,指向servle在web.xmlt映射的url。