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

简单验证码生成Java版

程序员文章站 2023-12-16 08:51:16
验证码大家都知道,它的作用也不用我多说了吧。如果不太清楚请参见百度百科中的解释,一般验证码的生成就是随机产生字符(数字、字母或者汉字等),然后将这些生成的字符绘制成一张图片...

验证码大家都知道,它的作用也不用我多说了吧。如果不太清楚请参见百度百科中的解释,一般验证码的生成就是随机产生字符(数字、字母或者汉字等),然后将这些生成的字符绘制成一张图片,再在图片上加上一些干扰元素,如各种线条之类的。好了废话不多说一起往下看:

简单验证码生成Java版

简单的登录页面(代码并不完整,只有验证码部分)

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> 
<html> 
 <head> 
 <title>验证码</title> 
 <script type="text/javascript"> 
<span style="white-space:pre" data-filtered="filtered"></span><span style="white-space:pre"> </span> //刷新验证码的js函数 
<span style="white-space:pre"> </span>function changecode() { 
<span style="white-space:pre">  </span>var imgnode = document.getelementbyid("vimg");     
 
<span style="white-space:pre">  </span>//重新加载验证码,达到刷新的目的 
<span style="white-space:pre">  </span>imgnode.src = "servlet/authimageservlet?t=" + math.random(); // 防止浏览器缓存的问题  
<span style="white-space:pre"> </span>}  
 </script> 
 </head> 
 <body> 
 <form action="checkservlet" method="post"> 
  <label>输入验证码</label><br/> 
  <input type="text" name="randomcode"/><img id="vimg" title="点击更换" onclick="changecode();" src="servlet/authimageservlet"><br/> 
  <input type="submit" value="submit"> 
 </form> 
 </body> 
</html> 

通过session.getattribute("rand"),获得生成的验证码,然后跟用户输入的进行比较,再根据比较结果做相应的处理。

通过servlet生成验证码:

package com.util.servlet; 
 
import java.awt.color; 
import java.awt.font; 
import java.awt.graphics; 
import java.awt.image.bufferedimage; 
import java.io.ioexception; 
import java.util.random; 
 
import javax.imageio.imageio; 
import javax.servlet.servletexception; 
import javax.servlet.http.httpservlet; 
import javax.servlet.http.httpservletrequest; 
import javax.servlet.http.httpservletresponse; 
import javax.servlet.http.httpsession; 
 
public class authimageservlet extends httpservlet { 
 
 private static final string content_type = "text/html; charset=gb2312";  
 //设置字母的大小,大小  
 private font mfont = new font("times new roman", font.plain, 17);  
 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/jpeg");  
    
  int width=100, 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);  
  }  
 
  //生成随机数,并将随机数字转换为字母  
  string srand="";  
  for (int i=0;i<6;i++)  
  {  
   int itmp = random.nextint(26) + 65;  
   char ctmp = (char)itmp;  
   srand += string.valueof(ctmp);  
   g.setcolor(new color(20+random.nextint(110),20+random.nextint(110),20+random.nextint(110)));  
   g.drawstring(string.valueof(ctmp),15*i+10,16);  
  }  
 
  httpsession session = request.getsession(true);  
  session.setattribute("rand",srand);  
  g.dispose();  
  imageio.write(image, "jpeg", response.getoutputstream());  
 }  
 
} 

web.xml中的servlet配置信息:

<?xml version="1.0" encoding="utf-8"?> 
<web-app version="2.5" 
 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_2_5.xsd"> 
  
 <servlet> 
 <servlet-name>authimageservlet</servlet-name> 
 <servlet-class>com.util.servlet.authimageservlet</servlet-class> 
 </servlet> 
 <servlet-mapping> 
 <servlet-name>authimageservlet</servlet-name> 
 <url-pattern>/servlet/authimageservlet</url-pattern> 
 </servlet-mapping> 
 
 <welcome-file-list> 
 <welcome-file>index.jsp</welcome-file> 
 </welcome-file-list> 
</web-app> 

就这样验证码的功能就实现了,很简单、很实用。当然还有更加美观,更加严密的方式,有兴趣的朋友可以深入研究。这里就跟大家分享一个比较简单的方式。欢迎各位批评指正。

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

上一篇:

下一篇: