Code structure:
Web. xml
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.action
ImageServlet
com.wzs.ImageServlet
ImageServlet
/http://www.2cto.com/uploadfile/Collfiles/20140617/20140617083740124.
index.jsp
Struts. xml
/Welcome. jsp
.
ImageServlet. java
Package com. wzs; import java. io. IOException; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class ImageServlet extends HttpServlet {private static final long serialVersionUID = 1L; public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setContentType ("image/jpeg"); // set the corresponding type to notify the browser that the output content is image response. setHeader ("Pragma", "No-cache"); // sets the response header to tell the browser not to cache the response. setHeader ("Cache-Control", "no-cache"); response. setDateHeader ("Expire", 0); RandomValidateCode randomValidateCode = new RandomValidateCode (); try {randomValidateCode. getRandcode (request, response); // method of output image} catch (Exception e) {e. printStackTrace () ;}} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet (request, response );}}
RandomValidateCode. java
Package com. wzs; import java. awt. color; import java. awt. font; import java. awt. graphics; import java. awt. image. bufferedImage; import java. util. random; import javax. imageio. imageIO; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import javax. servlet. http. httpSession; public class RandomValidateCode {public static final String RANDOMCODEKEY = "RANDOMVALIDATECODEKEY"; // keyprivate Random random = new Random (); private String randString = "0123456789 Random "; // randomly generated string private int width = 120; // picture width private int height = 26; // picture height private int lineSize = 40; // Number of interfering lines private int stringNum = 6; // Number of randomly generated characters/** obtain the Font */private Font getFont () {return new Font ("Fixedsys", Font. CENTER_BASELINE, 18);}/** obtain Color */private Color getRandColor (int fc, int bc) {if (fc> 255) fc = 255; if (bc> 255) bc = 255; int r = fc + random. nextInt (bc-fc-16); int g = fc + random. nextInt (bc-fc-14); int B = fc + random. nextInt (bc-fc-18); return new Color (r, g, B);}/*** generate random image */public void getRandcode (HttpServletRequest request, HttpServletResponse response) {HttpSession session = request. getSession (); // The BufferedImage class is an Image class with a buffer. The Image class is a class used to describe image information. BufferedImage Image = new BufferedImage (width, height, BufferedImage. TYPE_INT_BGR); Graphics g = image. getGraphics (); // generate the Graphics object of the Image object. You can perform various painting operations on the Image by modifying the object. fillRect (0, 0, width, height); g. setFont (new Font ("Times New Roman", Font. ROMAN_BASELINE, 18); g. setColor (getRandColor (110,133); // draw the interference line for (int I = 0; I <= lineSize; I ++) {drowLine (g );} // draw a random String randomString = ""; for (int I = 1; I <= stringNum; I ++) {randomString = drowString (g, randomString, I );} session. removeAttribute (RANDOMCODEKEY); session. setAttribute (RANDOMCODEKEY, randomString); g. dispose (); try {ImageIO. write (image, "JPEG", response. getOutputStream (); // outputs the images in the memory to the client in the form of flow} catch (Exception e) {e. printStackTrace () ;}/ ** draw String */private String drowString (Graphics g, String randomString, int I) {g. setFont (getFont (); g. setColor (new Color (random. nextInt (101), random. nextInt (111), random. nextInt (121); String rand = String. valueOf (getRandomString (random. nextInt (randString. length (); randomString + = rand; g. translate (random. nextInt (3), random. nextInt (3); g. drawString (rand, 13 * I, 16); return randomString;}/** draw interference line */private void drowLine (Graphics g) {int x = random. nextInt (width); int y = random. nextInt (height); int xl = random. nextInt (13); int yl = random. nextInt (15); g. drawLine (x, y, x + xl, y + yl);}/** get random characters */public String getRandomString (int num) {return String. valueOf (randString. charAt (num ));}}
LoginAction. java
Package com. wzs; import com. opensymphony. xwork2.ActionContext; import com. opensymphony. xwork2.ActionSupport; public class LoginAction extends ActionSupport {private String name; private String password; private String randomCode; @ Overridepublic String execute () throws Exception {ActionContext ctx = ActionContext. getContext (); String randomcodekey = (String) ctx. getSession (). get (RandomValidateCode. RANDOMCODEKEY); System. out. println ("username:" + getName (); System. out. println ("Password:" + getPassword (); System. out. println ("Image Display Verification Code:" + randomcodekey + "| user input verification code:" + randomCode); return null;} public String getName () {return name ;} public void setName (String name) {this. name = name;} public String getPassword () {return password;} public void setPassword (String password) {this. password = password;} public String getRandomCode () {return randomCode;} public void setRandomCode (String randomCode) {this. randomCode = randomCode ;}}
Index. jsp
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>Verification Code<Script type = "text/javascript"> function refresh (obj) {obj. src = "http://www.2cto.com/uploadfile/Collfiles/20140617/20140617083740124 .? "+ Math. random () ;}</script>
Welcome. jsp
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %>My JSP 'Welcome. jsp 'starting pageLogin successful!