验证码
主要:绘制,验证
在一些网站的注册页面,通常在最后会让用户输入验证码,以此防止恶意注册,有些也会在登陆页面,防止暴力破解密码等.
页面的验证码,实际上是一个图片,这个图片事先人们是不知道的,它是由程序随机绘制成的,并且,在产生的时候,程序记录下来了它的值.(便于后面的验证)
servlet 服务器小程序,可以实现绘制验证码的功能,绘制的步骤如下:
1.建立图片缓存对象
BufferImage bi = new BufferImage(width,height,绘制类型参数);
原型:BufferedImage(arg0, arg1, arg2)
2.得到绘制对象
Graphics g = bi.getGraphics();
3.设置底纹颜色
Color c = new Color(arg0,arg1,arg2);// 3个参数分别是三原色的值.
g.setColor(c);
g.fillRect(0, 0, 70, 30); //填充矩形,0,0 表示的起始点.
4.产生随机数字,得到随机字符,对图像进行绘制 (并且记录下来)
字符区间:char[] ch = "asdfghjklzxcvbnmASDFGHJKLZXCVBNM123456789".toCharArray();
字符缓存:StringBuffer sb = new StringBuffer();
随机数 :Random r = new Random();
//逐个绘制随机字符 并设置特定的颜色
for(int i=0;i<4;i++)
{
int index = r.nextInt(ch.length()); //产生一个随机数
//对字符分别设置绘制字体颜色
g.setFont(new Font("宋体",Font.Bold,20)); //设置宋体,20号字体
g.setColor(new Color(r.nexInt(255),r.nexInt(255),r.nexInt(255))); //设置随机颜色,
g.drawString(ch[index]+" ",i*20,25); //绘制字符,后面的参数为位置
sb.append(ch[index]); //添加字符到字符缓存区,(追加)
}
request.getSession().setAttribute("picCode",sb.toString()); //将验证码放到session中,方便后面的验证
5.通过图片IO写到前台
ImageIO.write(bi,"JPG",response.getOutputStream()); //通过输出流写到前台, JPG格式
到这里就能够在页面上面看到随机生成的验证码图片了.
验证:
验证就是在注册界面或者是登陆界面活取验证码 ,然后从session中活取验证码,然后二者比较相等,验证是否输入正确.
从session中活取验证码
String checkCode = request.getSession().getAttribute("picCode");
从前台活取输入的验证码
String code = request.getParameter("code");
验证
if(checkCode.equalsIgnoreCase(code)==false) response.sendRedirect("register.jsp"); //验证不成功跳转到制定页面
至此,验证码的相关基础的内容已经完毕,
代码:
myImage.java (servlet程序)
1 package cn.hpu.servlet; 2 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.Graphics; 6 import java.awt.image.BufferedImage; 7 import java.io.IOException; 8 import java.nio.Buffer; 9 import java.util.Random; 10 11 import javax.imageio.ImageIO; 12 import javax.servlet.ServletException; 13 import javax.servlet.annotation.WebServlet; 14 import javax.servlet.http.HttpServlet; 15 import javax.servlet.http.HttpServletRequest; 16 import javax.servlet.http.HttpServletResponse; 17 18 import sun.net.NetworkServer; 19 20 /** 21 * Servlet implementation class myImage 22 */ 23 @WebServlet("/myImage") 24 public class myImage extends HttpServlet { 25 private static final long serialVersionUID = 1L; 26 27 /** 28 * @see HttpServlet#HttpServlet() 29 */ 30 public myImage() { 31 super(); 32 // TODO Auto-generated constructor stub 33 } 34 35 /** 36 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 37 */ 38 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 39 // TODO Auto-generated method stub 40 // response.getWriter().append("Served at: ").append(request.getContextPath()); 41 doPost(request, response); 42 43 } 44 45 /** 46 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 47 */ 48 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 49 // TODO Auto-generated method stub 50 // doGet(request, response); 51 request.setCharacterEncoding("UTF-8"); 52 response.setCharacterEncoding("UTF-8"); 53 54 BufferedImage bi =new BufferedImage(70,30, BufferedImage.TYPE_INT_RGB); 55 Graphics g = bi.getGraphics(); 56 Color c = new Color(100, 190,180); 57 g.setColor(c); 58 g.fillRect(0, 0, 70, 30); 59 60 61 char[] ch = "asdfghjklzxcvbnmASDFGHJKLZXCVBNM123456789".toCharArray(); 62 StringBuffer sb= new StringBuffer(); 63 Random r = new Random(); 64 65 for (int i = 0; i < 4; i++) { 66 int index = r.nextInt(ch.length); 67 Font f = new Font("宋体",Font.BOLD, 20); 68 g.setFont(f); 69 g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255))); 70 g.drawString(ch[index]+" ", i*18+3, 20); 71 sb.append(ch[index]); 72 } 73 request.getSession().setAttribute("picCode", sb.toString()); 74 ImageIO.write(bi, "JPG", response.getOutputStream()); 75 76 77 } 78 79 }
register.java (servlet程序)
1 package cn.hpu.servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.sql.Date; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import org.apache.catalina.connector.Response; 14 15 import cn.hpu.model.user; 16 import cn.hpu.service.StuManager; 17 import cn.hpu.service.StuManagerimplement; 18 19 /** 20 * Servlet implementation class register 21 */ 22 @WebServlet("/register") 23 public class register extends HttpServlet { 24 private static final long serialVersionUID = 1L; 25 26 /** 27 * @see HttpServlet#HttpServlet() 28 */ 29 public register() { 30 super(); 31 // TODO Auto-generated constructor stub 32 } 33 34 /** 35 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 36 */ 37 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 38 // TODO Auto-generated method stub 39 response.getWriter().append("Served at: ").append(request.getContextPath()); 40 doPost(request, response); 41 } 42 43 /** 44 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 45 */ 46 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 47 // TODO Auto-generated method stub 48 // doGet(request, response); 49 //设置中文 50 request.setCharacterEncoding("UTF-8"); 51 response.setCharacterEncoding("UTF-8"); 52 53 //获取数据 54 String name = request.getParameter("name"); 55 String pwd = request.getParameter("password"); 56 String code = request.getParameter("code"); 57 String checkCode = (String) request.getSession().getAttribute("picCode"); 58 59 user usr = new user(); 60 usr.setName(name); 61 usr.setCode(code); 62 usr.setPassword(pwd); 63 64 if(checkCode.equalsIgnoreCase(code)==false){ 65 66 // PrintWriter out = response.getWriter(); 67 // out.println("验证码错误!\n3秒后自动跳转注册页面"); 68 69 response.sendRedirect("register.jsp"); 70 } 71 72 StuManager stuM = new StuManagerimplement(); 73 boolean flag= false ; 74 try { 75 flag = stuM.add(usr); 76 if(flag) response.sendRedirect("index.jsp"); 77 78 } catch (Exception e) { 79 // TODO: handle exception 80 } 81 } 82 83 }