사례 (다크호스) - 간단 한 인증 코드

package com.qgl.web.servlet;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
    private int width = 100;
    private int height = 50;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //       ,        
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

        //     
        //     
        Graphics g = image.getGraphics();   //     
        g.setColor(Color.PINK);             //       
        g.fillRect(0,0,width,height);
        //    
        g.setColor(Color.BLUE);
        g.drawRect(0,0,width-1,height-1);
        //      
        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random rd = new Random();
        //     
        for (int i = 1; i <8 ; i++) {
            int x1 = rd.nextInt(width);
            int x2 = rd.nextInt(width);
            int y1 = rd.nextInt(height);
            int y2 = rd.nextInt(height);
            g.drawLine(x1,x2,y1,y2);
        }
        //     
        g.setColor(Color.BLACK);
        for (int i = 1; i <=4; i++) {
            int index = rd.nextInt(str.length());
            char c = str.charAt(index);
            g.drawString(c+"",width/5*i,height/2);
        }

        //          
        ImageIO.write(image,"jpg",resp.getOutputStream());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
<html>
  <head>
    <title>checkcodetitle>
    <script>
      //     
      window.onload = function () {
          //           
          var change1 = document.getElementById("clickimg");
          var change2 = document.getElementById("clickchar");
          //             
          change1.onclick = function () {
              //      
              var time = new Date().getTime();
              change1.src = "/case/checkCodeServlet?"+time;
          }
          change2.onclick = function () {
              var time = new Date().getTime();
              change1.src = "/case/checkCodeServlet?"+time;
          }
      }
    script>
  head>
  <body>
    <img id="clickimg" src="/case/checkCodeServlet">
    <a id="clickchar" href="">a>
  body>
html>

좋은 웹페이지 즐겨찾기