Before want to do a random verification code function, I also searched for someone else to write code, and then re-use the struts2 to achieve a bit, now will I implement code to paste out! Everyone can point out any comments!
The first is the action that generates the random captcha picture:
Createimageaction:
Package Com.xiaoluo.action;import Java.awt.color;import Java.awt.font;import java.awt.graphics;import Java.awt.image.bufferedimage;import Java.io.bytearrayinputstream;import Java.io.bytearrayoutputstream;import Java.util.random;import Javax.imageio.imageio;import Javax.servlet.http.httpservletresponse;import Javax.servlet.http.httpsession;import Org.apache.struts2.ServletActionContext; Import Com.opensymphony.xwork2.actionsupport;public class Createimageaction extends actionsupport{private Bytearrayinputstream InputStream; private static int WIDTH = 60; private static int HEIGHT = 20; Public Bytearrayinputstream getInputStream () {return inputstream; } public void Setinputstream (Bytearrayinputstream inputstream) {this.inputstream = InputStream; }
private static string Createrandom () {string str = "0123456789QWERTYUIOPASDFGHJKLZXCVBNM"; char[] Rands = new Char[4]; Random random = new random (); for (int i = 0; i < 4; i++) {Rands[i] = Str.charat (Random.nextint (36)); } return new String (Rands); } private void Drawbackground (Graphics g) {//Draw background G.setcolor (new Color (0xDCDCDC)); G.fillrect (0, 0, WIDTH, HEIGHT); Randomly generates 120 interference points for (int i = 0; i < i++) {int x = (int) (Math.random () * WIDTH); int y = (int) (Math.random () * HEIGHT); int red = (int) (Math.random () * 255); int green = (int) (Math.random () * 255); int blue = (int) (Math.random () * 255); G.setcolor (New Color (red, green, blue)); G.drawoval (x, y, 1, 0); }} private void Drawrands (Graphics g, String rands) {g.setcolor (color.black); G.setfonT (new Font (NULL, Font.Italic | Font.Bold, 18)); Each character g.drawstring ("" + rands.charat (0), 1, 17) of the output verification code at different heights; g.DrawString ("" + rands.charat (1), 16, 15); g.DrawString ("" + Rands.charat (2), 31, 18); g.DrawString ("" + Rands.charat (3), 46, 16); System.out.println (Rands); } @Override Public String execute () throws Exception {HttpServletResponse response = Servletactioncontext . GetResponse (); Set browser do not cache this picture Response.setheader ("Pragma", "No-cache"); Response.setheader ("Cache-control", "No-cache"); Response.setdateheader ("Expires", 0); String rands = Createrandom (); BufferedImage image = New BufferedImage (WIDTH, HEIGHT, Bufferedimage.type_int_rgb); Graphics g = image.getgraphics (); Generate Image Drawbackground (g); Drawrands (g, rands); End the drawing process of the image and complete the image g.dispose (); Bytearrayoutputstream outputstream = new Bytearrayoutputstream (); Imageio.write (image, "JPEG", outputstream); Bytearrayinputstream input = new Bytearrayinputstream (outputstream. Tobytearray ()); This.setinputstream (input); HttpSession session = Servletactioncontext.getrequest (). GetSession (); Session.setattribute ("Checkcode", rands); Input.close (); Outputstream.close (); return SUCCESS; }}
The above is the action that generates a random captcha image, the generated random number is placed in the session, and the page is submitted to the action that validates the random number:
Loginvalidateaction:
Package Com.xiaoluo.action;import Javax.servlet.http.httpsession;import Org.apache.struts2.ServletActionContext; Import Com.opensymphony.xwork2.actionsupport;public class Loginvalidateaction extends actionsupport{ private String Checkcode; Public String Getcheckcode () { return checkcode; } public void Setcheckcode (String checkcode) { this.checkcode = Checkcode; } @Override public String execute () throws Exception { return SUCCESS; } @Override public Void Validate () { HttpSession session = Servletactioncontext.getrequest (). GetSession (); String checkCode2 = (string) session.getattribute ("Checkcode"); if (!checkcode.equals (checkCode2)) { this.addactionerror ("Input verification code is not correct, please re-enter!") "); } }}
Here is the Struts.xml configuration section code:
<action name= "createimageaction" class= "com.xiaoluo.action.CreateImageAction" > <result name= "success "Type=" stream "> <param name=" ContentType ">image/jpeg</param> <param name=" InputName " >inputStream</param> </result> </action> <action name= "Loginvalidateaction" class= "Com.xiaoluo.action.LoginValidateAction" > <result name= "Success" >/success.jsp</result> <result name= "input" >/login.jsp</result> </action>
The last is the JSP part of the code:
LOGIN.JSP:
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%@ taglib prefix= "s" uri= "/struts-tags" %><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%>
<!--to click on the image to refresh, to get a verification code, to add a random number, so that each commit the past is a different path, to prevent the cache so that the picture is not refreshed--<br> <s:actionerror cssstyle=" color:red "/> <s: Submit Value= "Submissions" ></s:submit> </s:form> </body>Struts2 generate random Verification code picture