登录图片验证

自己的验证图片生成servlet,还有相关的配置和JSP。先留个脚印

 

package com.thomas.logoimagecheck;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ValidateCodeServlet extends HttpServlet {
 // 验证码宽和高及验证码的位数
 private int width = 80;

 private int height = 30;

 private int codecount = 4;

 private int fontheight;

 private int x;

 private int codeY;

 char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
   'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
   'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

 public void inti() throws ServletException {
  // 从web.xml文件中读取参数
  String strWidth = this.getInitParameter("width");
  String strHeight = this.getInitParameter("height");
  String codeCount = this.getInitParameter("codeCount");
  try {
   if (strWidth != null && strWidth.length() != 0) {
    width = Integer.getInteger(strWidth);
   }
   if (strHeight != null && strHeight.length() != 0) {
    height = Integer.parseInt(strHeight);
   }
   if (codeCount != null && codeCount.length() != 0) {
    codecount = Integer.parseInt(codeCount);
   }
  } catch (NumberFormatException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

protected void service(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  // 定义图像buffer
  x = width / (codecount + 1);
  fontheight = height - 4;
  codeY = height - 4;
  BufferedImage bimage = new BufferedImage(width, height,
    BufferedImage.TYPE_INT_RGB);
  Graphics2D g = bimage.createGraphics();
  Random random = new Random();
  g.setColor(Color.white);
  g.fillRect(0, 0, width, height);
  // 创建字体,字体的大小应该根据图片的高度来定。(fontheight实现了)
  Font font = new Font("Times New Roman",Font.PLAIN,fontheight);
  // 设置画的边框
  g.setFont(font);
  g.setColor(new Color(110,110,110));
  g.drawRect(0, 0, width - 1, height - 1);
  // 随机产生160条干扰线
  //g.setColor(Color.BLACK);
  for (int i = 0; i < 160; i++) {
   int x = random.nextInt(160);
   int y = random.nextInt(160);
   int xl = random.nextInt(12);
   int yl = random.nextInt(12);
   g.drawLine(x, y, x + xl, y + yl);
  }
  StringBuffer randomcode = new StringBuffer();
  int red = 0, green = 0, blue = 0;
  // 随机产生验证码
  for (int i = 0; i < codecount; i++) {
   // 产生验证码的数字
   String strRand = String.valueOf(codeSequence[random.nextInt(36)]);
   System.out.print(strRand);
   red=20+random.nextInt(110);
   green=20+random.nextInt(110);
   blue=20+random.nextInt(110);
   g.setColor(new Color(red,green,blue));
   g.drawString(strRand,18*i+10,codeY);
   randomcode.append(strRand);
  }
  //把字符串存储到session中
  HttpSession session=req.getSession();
  session.setAttribute("valibaleCode", randomcode.toString());
  resp.setHeader("Pragma","no-cache");
  resp.setHeader("Cache-Control","no-cache");
  resp.setContentType("image/jpeg");
  ServletOutputStream sos=resp.getOutputStream();
  ImageIO.write(bimage, "jpeg", sos);
  sos.close();
 }
}

 

下面是验证的jsp

 

<body>
  <% String str=(String)session.getAttribute("valibaleCode");
   String imagestr=request.getParameter("rand");
   //out.print(request.getParameter("rand"));
  //out.print(str);
   if(!imagestr.equalsIgnoreCase(str)){
    out.print("unpassed");
   }
   if(imagestr.equalsIgnoreCase(str)){
    out.print("passed ");
   }
   %>
  <form method=post action="logoncheck/logon.jsp">
    <img border=0 src="<%=basePath %>validate">
    <input type=text name=rand value=""/>
    <input type=submit value="check"/>
    </form>
  </body>

 

web.xml文件

<servlet-name>ValidateCodeServlet</servlet-name>
 <servlet-class>com.thomas.logoimagecheck.ValidateCodeServlet</servlet-class>
 <init-param>
 <param-name>width</param-name>
 <param-value>200</param-value>
 </init-param>
 <init-param>
 <param-name>height</param-name>
 <param-value>80</param-value>
 </init-param>
 <init-param>
 <param-name>codeCount</param-name>
 <param-value>5</param-value>
 </init-param>
 </servlet>
  
  
   <servlet-mapping>
   <servlet-name>ValidateCodeServlet</servlet-name>
   <url-pattern>/validate</url-pattern>
   </servlet-mapping>

 

你可能感兴趣的:(jsp,xml,Web,cache,servlet)