Manually make java verification code
The composition of the Web application verification code :
(1) Input box
(2) The picture showing the verification code
The production process of verification code :
The container that generates the verification code uses j2ee's servlet
Classes required to generate images :
(1) BufferedImage image data buffer
(2) Graphic drawing pictures
(3) Color gets the color
(4) Random generates random numbers
(5) ImageIO input picture
The specific implementation process:
(1) Define a Servlet to generate verification code
(2) The BufferedImage object has been determined, and the main function is to create a picture buffer as a temporary container for the picture.
(3) Obtain the Graphic object, the "background" of the drawing, understood as "canvas"
(4) Generate random numbers by Random to make verification information
(5) Perform specific drawing through Graphic operation
(6) The information is stored in the session
(7) Use ImageIO to output the generated image, and transfer the image to the foreground by setting the out parameter in the write() method of ImageIO, response.getOutputStream()
(8) Make a servlet for verification, and extract the data in the session for verification (ajax asynchronous method is used here)
Specific code implementation (Web-side verification code example)
1 <html> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 4 <title>java验证码</title> 5 <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> 6 </head> 7 <body> 8 验证码输入:<input type="text" name="checkcode"> 9 <img id="codeimg" alt="验证码" src="ImageServlet"> 10 <a href="javascript:reloadCode();">看不出清楚?</a><br/> 11 <input type="submit" value="提交"> 12 <div id="info" style="color:red;"></div> 13 14 <script type="text/javascript"> 15 $(function(){ 16 //ajax异步传验证码至后台 17 $("input[type=submit]").click(function(){ 18 $.post("CheckCode", 19 {"code":$("input[name=checkcode]").val()}, 20 function(data,textStatus) 21 { 22 console.log(textStatus); 23 $( " #info " ).html(data); 24 }, " text " ); 25 }); 26 }) 27 // js refresh, re-request the page, get a new verification code 28 function reloadCode(){ 29 var time = new Date().getTime(); // create a different time 30 $( " #codeimg " ).attr( " src " , " ImageServlet?time= " + time); //Because the time parameter is different, the request is re- 31 } 32 </ script > 33 </ body > 34 </ html >
ImageServlet class (captcha generation part)
@WebServlet("/ImageServlet") public class ImageServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ImageServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub BufferedImage bimg = new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB); Graphics g = bimg.getGraphics(); Color color = new Color(200,151,255);//颜色生成 g.setColor(color); g.fillRect( 0, 0, 68, 22 ); char [] ch="ABCDEFGHJIKLMNOPQRSTUVWXYZ0123456789".toCharArray(); // Get an array containing letters and numbers, and then randomly get characters from it Random random = new Random(); // Create a random number int len = ch.length,index; StringBuffer sBuffer = new StringBuffer(); // loop to generate 4 random characters for ( int i = 0 ;i<4;i++ ) { index = random.nextInt(len); // Generate random letters and numbers g.setColor ( new Color(random.nextInt(88),random.nextInt(120),random.nextInt(90))); // create random color g.drawString(ch[index]+"", (i)*15+3, 18 ); sBuffer.append(ch[index]); // Place randomly acquired characters in the buffer string } request.getSession().setAttribute( "newCode", sBuffer.toString()); // Save to session for subsequent verification ImageIO.write(bimg, "JPG", response.getOutputStream()); // Output pictures through ImageIO , and pass it to the front desk } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
CheckCode (Verification Code Verification servlet)
@WebServlet("/CheckCode") public class CheckCode extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public CheckCode() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setCharacterEncoding("utf-8"); String code = request.getParameter("code").toUpperCase(); // Get the verification code and convert the case String result= null ; if (request.getSession().getAttribute("newCode" ).equals( code)) { result = "Verification successful!" ; response.getWriter().append(result); } else { result = "Verification code error!" ; response.getWriter().append(result); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }