摘要: 网页上的表单经常需要用到验证码,验证码是以图片的形式显示的,图片由后台Servlet生成,通过URI链接显示在网页上。 首先说明一下随机验证码的流程。
从流程图可以看出,我们需要做一下几步工作。
- 通过后台Servlet生成随机图片。
- 创建Session会话,并通过登录页面进行验证。
- 刷新随机图片超链接后,更换验证码图片。
002 | * To change this template, choose Tools | Templates |
003 | * and open the template in the editor. |
005 | package net.individuals.web.servlet; |
007 | import java.awt.Color; |
009 | import java.awt.Graphics; |
010 | import java.awt.image.BufferedImage; |
011 | import java.io.IOException; |
012 | import java.util.Random; |
013 | import javax.imageio.ImageIO; |
014 | import javax.servlet.ServletException; |
015 | import javax.servlet.annotation.WebServlet; |
016 | import javax.servlet.http.HttpServlet; |
017 | import javax.servlet.http.HttpServletRequest; |
018 | import javax.servlet.http.HttpServletResponse; |
024 | @WebServlet (name = "ImageCodeMakerServlet" , urlPatterns = { "/ImageCodeMakerServlet" }) |
025 | public class ImageCodeMakerServlet extends HttpServlet { |
028 | * Processes requests for both HTTP |
029 | * <code>GET</code> and |
030 | * <code>POST</code> methods. |
032 | * @param request servlet request |
033 | * @param response servlet response |
034 | * @throws ServletException if a servlet-specific error occurs |
035 | * @throws IOException if an I/O error occurs |
037 | protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
038 | throws ServletException, IOException { |
040 | response.setHeader( "Pragma" , "No-cache" ); |
041 | response.setHeader( "Cache-Control" , "No-cache" ); |
043 | response.setDateHeader( "Expires" , 0 ); |
046 | int width = 60 , height = 20 ; |
049 | BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); |
052 | Graphics g = image.createGraphics(); |
055 | Random random = new Random(); |
058 | g.setColor( new Color( 255 , 255 , 255 )); |
061 | g.fillRect( 0 , 0 , width, height); |
064 | g.setFont( new Font( "Tahoma" , Font.PLAIN, 20 )); |
069 | for ( int i = 0 ; i < 4 ; i++) { |
071 | String rand = String.valueOf(random.nextInt( 10 )); |
074 | g.setColor( new Color( 100 + random.nextInt( 155 ), random.nextInt( 100 ), 100 + random.nextInt( 155 ))); |
076 | g.drawString(rand, ( 12 + random.nextInt( 2 )) * i + 6 , 18 ); |
080 | for ( int k = 0 ; k < 24 ; k++) { |
081 | g.setColor(getRandomColor( 0 , 255 )); |
082 | int x = random.nextInt(width); |
083 | int y = random.nextInt(height); |
084 | int x1 = random.nextInt( 6 ); |
085 | int y1 = random.nextInt( 6 ); |
086 | g.drawLine(x, y, x + x1, y + y1); |
090 | for ( int i = 0 ; i < 10 ; i++) { |
091 | g.setColor(getRandomColor( 50 , 180 )); |
092 | int x = random.nextInt(width); |
093 | int y = random.nextInt(height); |
094 | g.drawOval(x, y, 1 , 1 ); |
098 | request.getSession().setAttribute( "randcode" , sRand); |
104 | ImageIO.write(image, "JPEG" , response.getOutputStream()); |
115 | public Color getRandomColor( int fc, int bc) { |
116 | Random random = new Random(); |
124 | int r = fc + random.nextInt(bc - fc); |
125 | int g = fc + random.nextInt(bc - fc); |
126 | int b = fc + random.nextInt(bc - fc); |
128 | return new Color(r, g, b); |
134 | * <code>GET</code> method. |
136 | * @param request servlet request |
137 | * @param response servlet response |
138 | * @throws ServletException if a servlet-specific error occurs |
139 | * @throws IOException if an I/O error occurs |
142 | protected void doGet(HttpServletRequest request, HttpServletResponse response) |
143 | throws ServletException, IOException { |
144 | processRequest(request, response); |
149 | * <code>POST</code> method. |
151 | * @param request servlet request |
152 | * @param response servlet response |
153 | * @throws ServletException if a servlet-specific error occurs |
154 | * @throws IOException if an I/O error occurs |
157 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
158 | throws ServletException, IOException { |
159 | processRequest(request, response); |
163 | * Returns a short description of the servlet. |
165 | * @return a String containing servlet description |
168 | public String getServletInfo() { |
169 | return "Short description" ; |
实现点击图片更新随机验证码。需要在URL上加上Math.random(),通过不同参数的URL地址响应,否则访问同一个URL资源将不更新。
1 | $( '#verification' ).click( function () { |
2 | $( '#verification' ).attr( 'src' , 'ImageCodeMakerServlet?' + Math.random()); |