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

java使用servlet实现验证码

程序员文章站 2024-03-08 15:07:40
利用servlet 实现验证码主要继承httpservlet类 package com.zyc.demo; import java.awt.colo...

利用servlet 实现验证码主要继承httpservlet类

package com.zyc.demo; 
 
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 drewimage extends httpservlet{ 
  /** 
   * 
   */ 
  private static final long serialversionuid = 1505032428319459075l; 
  private final font mfont = 
      new font("arial black", font.plain, 16); 
    private final int img_width = 100; 
    private final int img_heigth = 18; 
    private 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"); 
      bufferedimage image = new bufferedimage 
        (img_width , img_heigth , bufferedimage.type_int_rgb); 
      graphics g = image.getgraphics(); 
      random random = new random(); 
      g.setcolor(getrandcolor(200 , 250)); 
      g.fillrect(1, 1, img_width - 1, img_heigth - 1); 
      g.setcolor(new color(102 , 102 , 102)); 
      g.drawrect(0, 0, img_width - 1, img_heigth - 1); 
      g.setcolor(getrandcolor(160,200)); 
      for (int i = 0 ; i < 30 ; i++) 
      { 
        int x = random.nextint(img_width - 1); 
        int y = random.nextint(img_heigth - 1); 
        int xl = random.nextint(6) + 1; 
        int yl = random.nextint(12) + 1; 
        g.drawline(x , y , x + xl , y + yl); 
      } 
      g.setcolor(getrandcolor(160,200)); 
      for (int i = 0 ; i < 30 ; i++) 
      { 
        int x = random.nextint(img_width - 1); 
        int y = random.nextint(img_heigth - 1); 
        int xl = random.nextint(12) + 1; 
        int yl = random.nextint(6) + 1; 
        g.drawline(x , y , x - xl , y - yl); 
      } 
      g.setfont(mfont); 
      string srand = ""; 
      for (int i = 0 ; i < 4 ; i++) 
      { 
        string tmp = getrandomchar(); 
        srand += tmp; 
        g.setcolor(new color(20 + random.nextint(110) 
          ,20 + random.nextint(110) 
          ,20 + random.nextint(110))); 
        g.drawstring(tmp , 15 * i + 10,15); 
      } 
      httpsession session = request.getsession(true); 
      session.setattribute("rand" , srand); 
//     system.out.println("写入session"+srand); 
      g.dispose(); 
      imageio.write(image, "jpeg", response.getoutputstream()); 
    } 
    private string getrandomchar() 
    { 
      int rand = (int)math.round(math.random() * 2); 
      long itmp = 0; 
      char ctmp = '\u0000'; 
      switch (rand) 
      { 
        case 1: 
          itmp = math.round(math.random() * 25 + 65); 
          ctmp = (char)itmp; 
          return string.valueof(ctmp); 
        case 2: 
          itmp = math.round(math.random() * 25 + 97); 
          ctmp = (char)itmp; 
          return string.valueof(ctmp); 
        default : 
          itmp = math.round(math.random() * 9); 
          return itmp + ""; 
      } 
    } 
} 

下面是web.xml 配置

<?xml version="1.0" encoding="utf-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="webapp_id" version="3.0"> 
 <display-name>industrydemo</display-name> 
 <welcome-file-list> 
  <welcome-file>index.html</welcome-file> 
  <welcome-file>index.htm</welcome-file> 
  <welcome-file>index.jsp</welcome-file> 
  <welcome-file>default.html</welcome-file> 
  <welcome-file>default.htm</welcome-file> 
  <welcome-file>default.jsp</welcome-file> 
 </welcome-file-list> 
  
 <servlet> 
   <servlet-name>img</servlet-name> 
   <servlet-class>com.zyc.demo.drewimage</servlet-class> 
 </servlet> 
  
 <servlet-mapping> 
   <servlet-name>img</servlet-name> 
   <url-pattern>/img.do</url-pattern> 
 </servlet-mapping> 
  
</web-app> 

 jsp 文件

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> 
<% 
string path = request.getcontextpath(); 
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; 
%> 
 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> 
<html> 
 <head> 
  <base href="<%=basepath%>"> 
   
  <title>my jsp 'yanzhengma.jsp' starting page</title> 
   
  <meta http-equiv="pragma" content="no-cache"> 
  <meta http-equiv="cache-control" content="no-cache"> 
  <meta http-equiv="expires" content="0">   
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
  <meta http-equiv="description" content="this is my page"> 
  <!-- 
  <link rel="stylesheet" type="text/css" href="styles.css"> 
  --> 
 
 </head> 
  
 <body> 
  <img alt="验证码" src="img.do"> <button onclick="window.location.reload();">刷新</button> 
 </body> 
</html> 

简单实用。

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