Two types of verification codes: verification Codes
Here I have written two types of verification codes. One is to randomly generate four digits, and the other is to add or subtract Chinese Text mathematical questions. In fact, it is a little different to generate images, just like in other places.
Html code
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Struts. xml Code
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><constant name="struts.i18n.encoding" value="UTF-8"></constant><package name="yzm" extends="struts-default"><result-types> <result-type name="ValidateImage" class="yzm.ImageResult" /> </result-types> <action name="ValidateImage" class="yzm.ImageAction" method="doDefault"> <result name="image" type="ValidateImage" /> </action> <action name="getzyms" class="com.web.actoin.getyzm" method="getzyms"> </action></package></struts>
Java code
package com.web.actoin;import org.apache.struts2.ServletActionContext;public class getyzm {public void getzyms(){System.out.println(ServletActionContext.getRequest().getSession().getAttribute("CheckCodeImageAction").toString().toUpperCase());}}
package yzm;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.Result;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;public class ImageResult implements Result { public void execute(ActionInvocation ai) throws Exception { ImageAction action = (ImageAction)ai.getAction(); HttpServletResponse response = ServletActionContext.getResponse(); response.setHeader("Cash", "no cash"); response.setContentType(action.getContentType()); response.setContentLength(action.getContentLength()); response.getOutputStream().write(action.getImageBytes()); response.getOutputStream().flush(); }}
You can select either of the following two values (four digits and addition or subtraction)
Package yzm; import com. opensymphony. xwork2.ActionSupport; import java. util. random; import java. awt. *; import java. awt. image. *; import java. io. byteArrayOutputStream; import org. apache. struts2.ServletActionContext; public class ImageAction extends ActionSupport {// private static final String SessionName = "CheckCodeImage"; private static final Random rdm = new Random (); private static final String [] china = new String [] {"0", "1", "2", "3", "4", "5 ", "6", "7", "8", "9"}; private static final String [] fh = new String [] {"plus", "minus ", "Multiply"}; public static Color getRandomColor () {return new Color (rdm. nextInt (255), rdm. nextInt (255), rdm. nextInt (255);} public static Color getReverseColor (Color c) {return new Color (255-c.getRed (), 255-c.getGreen (), 255-c.getBlue ());} private static final Font font = new Font (Font. SANS_SERIF, Font. BOLD, 16); private String text = ""; private byte [] bytes = null; private String contentType = "image/jpeg"; public byte [] getImageBytes () {return this. bytes;} public String getContentType () {return this. contentType;} public void setContentType (String value) {this. contentType = value;} public int getContentLength () {return bytes. length;} private void generateText () {String source = new String (); int f = rdm. nextInt (ImageAction. china. length); int s = rdm. nextInt (ImageAction. china. length); int h = rdm. nextInt (ImageAction. fh. length); source = ImageAction. china [f] + ImageAction. fh [h] + ImageAction. china [s]; int result = 0; if (h = 0) result = f + s; if (h = 1) result = f-s; if (h = 2) result = f * s; System. out. println ("gettext:" + source); this. text = new String (source); // sets the length of the Session ServletActionContext. getRequest (). getSession (). setAttribute (SessionName, result);} private BufferedImage createImage () {int width = 70; int height = 20; Color color = Color. BLACK; Color reverse = getReverseColor (color); BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB); Graphics2D g = image. createGraphics (); g. setFont (new Font (Font. SANS_SERIF, Font. BOLD, 16); g. setColor (color); g. fillRect (0, 0, width, height); g. setColor (reverse); System. out. println ("text:" + text); g. drawString (text, 15, 15); for (int I = 0, n = rdm. nextInt (20); I <n; I ++) {g. drawRect (rdm. nextInt (width), rdm. nextInt (height), 1, 1);} g. dispose (); return image;} private void generatorImageBytes (BufferedImage image) {ByteArrayOutputStream bos = new ByteArrayOutputStream (); try {javax. imageio. imageIO. write (image, "jpg", bos); this. bytes = bos. toByteArray ();} catch (Exception ex) {} finally {try {bos. close () ;}catch (Exception ex1) {}} public String doDefault () {this. generateText (); BufferedImage image = this. createImage (); this. generatorImageBytes (image); System. out. println ("_" + ServletActionContext. getRequest (). getSession (). getAttribute (SessionName); return "image ";}}
Or
Package yzm; import com. opensymphony. xwork2.ActionSupport; import java. util. random; import java. awt. *; import java. awt. image. *; import java. io. byteArrayOutputStream; import org. apache. struts2.ServletActionContext; public class Images extends ActionSupport {// generate an image of four digits/*** copy a copy of a copy"; private static final Random rdm = new Random (); /*** copy a copy of ', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D ', 'E', 'F', 'G', 'h', 'J', 'k', 'l', 'M', 'n', 'P ', 'Q', 'R', 's', 't', 'U', 'V', 'w', 'x', 'y ', 'Z'};/*** copy a copy of a copy. nextInt (255), rdm. nextInt (255), rdm. nextInt (255);} public static Color getReverseColor (Color c) {return new Color (255-c.getRed (), 255-c.getGreen (), 255-c.getBlue ());} /*** copy a copy of a copy. SANS_SERIF, Font. BOLD, 16);/*** click here to copy your mouse. Click here to copy your mouse. Click here to copy your mouse. */private String text = ""; private byte [] bytes = null; private String contentType = "image/jpeg"; public byte [] getImageBytes () {return this. bytes;} public String getContentType () {return this. contentType;} public void setContentType (String value) {this. contentType = value;} public int getContentLength () {return bytes. length;}/*** copy a copy of a slogan () {char [] source = new char [4]; for (int I = 0; I <source. length; I ++) {source [I] = Images. source [rdm. nextInt (Images. source. length)];} this. text = new String (source); // sets the length of the Session ServletActionContext. getRequest (). getSession (). setAttribute (SessionName, this. text);}/*** click here to copy the yarn and yarn.★Copy a piece of cake, copy a piece of cake, copy a copy of a piece of cake, copy a copy of a piece of cake, copy a piece bufferedImage createImage () {int width = 70; int height = 20; Color color = Color. BLACK; Color reverse = getReverseColor (color); BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB); Graphics2D g = image. createGraphics (); g. setFont (new Font (Font. SANS_SERIF, Font. BOLD, 16); g. setColor (color); g. fillRect (0, 0, width, height); g. setColor (reverse); g. drawString (text, 15, 15); for (int I = 0, n = rdm. nextInt (20); I <n; I ++) {g. drawRect (rdm. nextInt (width), rdm. nextInt (height), 1, 1);} g. dispose (); return image ;} /*** copy a piece of cake, a copy of it, a copy of it, and a copy of it */private void generatorImageBytes (BufferedImage image) {ByteArrayOutputStream bos = new ByteArrayOutputStream (); try {javax. imageio. imageIO. write (image, "jpg", bos); this. bytes = bos. toByteArray ();} catch (Exception ex) {} finally {try {bos. close ();} catch (Exception ex1) {}}/ **** duplicate copy struts2 duplicate copy * @ return duplicate copy Copy copy image/public String doDefault () {this. generateText (); BufferedImage image = this. createImage (); this. generatorImageBytes (image); System. out. println ("_" + ServletActionContext. getRequest (). getSession (). getAttribute (SessionName); return "image ";}}
Why is the Verification Code incorrect?
First case:
The entered verification code is not correct, but an error is still prompted, which may be due to the following two situations:
I. Page refresh issues
Every time you refresh the page, the verification code will change. If the system prompts that the verification code is incorrect, the system will change the Verification Code. At this time, the page may still be the previous verification code. You cannot enter the same verification code twice, after seeing the new verification code, you can enter the new verification code again;
2. Question about full/half-width of the Input Method
The full-width/half-width numbers in the input method are different, that is, the full-width numbers 1234 and the half-width number 1234 are different. The Sina Blog Verification Code only supports the half-width numbers, such as the full-width numbers, although the number is correct, it still cannot be verified;
In addition, as the number of new Sina Blog users increases, the system must be upgraded and maintained frequently. Unexpected problems may occur and some users may be affected;
Case 2:
1. Set the security level of your browser to medium;
2. Clear the browser cache;
3. Set the computer's time zone to Beijing time zone. In internet time, remove the check box before the automatic synchronization with the internet time server, and set the time and date correctly;
4. Close the browser, re-open the BLOG homepage, and do not open other BLOG pages;
5. When you click the login button, do not click it continuously;
Finally, be sure to pay attention to one thing: the verification code is time-effective, and the page will be automatically refreshed even if you haven't refreshed the page for a period of time. Therefore, do not log on at intervals of time, the verification code also changes;
How to generate a complex verification code?
English, numbers, or Chinese characters are not important, mainly character distortion, color mixing, line interference, and other measures. The best way is to test the color discrimination force. The style is perfect.