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

jsp页面验证码完整实例

程序员文章站 2023-11-13 18:26:58
本文实例为大家分享了sp页面验证码的具体代码,供大家参考,具体内容如下 项目结构如下,myeclipse中新建一个web project,取名servlet 1、s...

本文实例为大家分享了sp页面验证码的具体代码,供大家参考,具体内容如下

项目结构如下,myeclipse中新建一个web project,取名servlet

jsp页面验证码完整实例

1、src下new一个servlet类

package com.servlet;

import java.awt.color;
import java.awt.font;
import java.awt.graphics2d;
import java.awt.image.bufferedimage;
import java.io.ioexception;
import java.io.printwriter;
import java.util.random;

import javax.servlet.servletexception;
import javax.servlet.servletoutputstream;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import com.sun.image.codec.jpeg.jpegcodec;
import com.sun.image.codec.jpeg.jpegimageencoder;

public class identityservlet extends httpservlet {

  public static final char[] chars={'2','3','4','5','6','7','8','9','a'};//自定义验证码池
  public static random random=new random();  //随机数
  
  public static string getrandomstring(){  //获取6位随机数,放在图片里
    stringbuffer buffer=new stringbuffer();
    for(int i=0;i<6;i++){
      buffer.append(chars[random.nextint(chars.length)]);
    }
    return buffer.tostring();
  }
  
  public static color getrandomcolor(){  //获取随机的颜色
    return new color(random.nextint(255), random.nextint(255), random.nextint(255));
  }
  
  public static color getreversecolor(color c){  //返回某颜色的反色
    return new color(255 - c.getred(), 255 - c.getgreen(), 255 - c.getblue());
  }
  
  /**
   * constructor of the object.
   */
  public identityservlet() {
    super();
  }

  /**
   * destruction of the servlet. <br>
   */
  public void destroy() {
    super.destroy(); // just puts "destroy" string in log
    // put your code here
  }

  /**
   * the doget method of the servlet. <br>
   *
   * this method is called when a form has its tag value method equals to get.
   * 
   * @param request the request send by the client to the server
   * @param response the response send by the server to the client
   * @throws servletexception if an error occurred
   * @throws ioexception if an error occurred
   */
  public void doget(httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {

    response.setcontenttype("image/jpeg");  //设置输出类型
    
    string randomstring = getrandomstring();  //随机字符串
    request.getsession(true).setattribute("randomstring", randomstring);//放到session里
    
    int width=100;  //图片宽度
    int height=30;  //图片高度
    
    color color=getrandomcolor();  //随机颜色,用于背景色
    color reverse=getreversecolor(color);//反色,用于前景色
    //创建一个彩色图片
    bufferedimage bi=new bufferedimage(width, height, bufferedimage.type_int_rgb);
    graphics2d g=bi.creategraphics();  //绘图对象
    g.setfont(new font(font.sans_serif,font.bold,16));//设置字体
    g.setcolor(color);//设置颜色
    g.fillrect(0, 0, width, height);//绘制背景
    g.setcolor(reverse);
    g.drawstring(randomstring, 18, 20);//绘制随机字符
    for(int i=0,n=random.nextint(100);i<n;i++){  //画最多100个噪音点
      g.drawrect(random.nextint(width), random.nextint(height), 1, 1);
    }
    servletoutputstream out= response.getoutputstream();//转成jpeg格式
    jpegimageencoder encoder=jpegcodec.createjpegencoder(out);//编码器
    encoder.encode(bi);  //对图片进行编码
    out.flush();  //输出到客户端
  }

  /**
   * the dopost method of the servlet. <br>
   *
   * this method is called when a form has its tag value method equals to post.
   * 
   * @param request the request send by the client to the server
   * @param response the response send by the server to the client
   * @throws servletexception if an error occurred
   * @throws ioexception if an error occurred
   */
  public void dopost(httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {

    doget(request, response);
  }

  /**
   * initialization of the servlet. <br>
   *
   * @throws servletexception if an error occurs
   */
  public void init() throws servletexception {
    // put your code here
  }

}

2、web.xml,会自动生成servlet和servlet-mapping的配置

<?xml version="1.0" encoding="utf-8"?>
<web-app version="3.0" 
  xmlns="http://java.sun.com/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
  xsi:schemalocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <display-name></display-name>
 <servlet>
  <servlet-name>identityservlet</servlet-name>
  <servlet-class>com.servlet.identityservlet</servlet-class>
 </servlet>

 <servlet-mapping>
  <servlet-name>identityservlet</servlet-name>
  <url-pattern>/servlet/identityservlet</url-pattern>
 </servlet-mapping>  
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>

3、webroot下新建一个html,展示验证码

<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>insert title here</title>
<script type="text/javascript">
function reloadimage(){
  document.getelementbyid('btn').disabled=true;
  document.getelementbyid('identity').src='servlet/identityservlet?ts='+new date().gettime();
}
</script>
</head>

<body>

<img src="servlet/identityservlet" id="identity" onload="btn.disabled=false;" />
<input type=button value="换个图片" onclick="reloadimage()" id="btn">
</body>
</html>

启动tomcat,输入网址:http://localhost:8080/servlet/identity.html,效果如下:

jsp页面验证码完整实例

点击‘换个图片',会生成新的验证码。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。