About to learn about the relevant API
BufferedImage(int width, int height, int imageType) Constructs one of the predefined image types for a type BufferedImage .
BufferedImage describes a buffer that has access to the image dataImage 字段摘要(TYPE_INT_RGB)表示一个图像,它具有合成整数像素的 8 位 RGB 颜色分量。
getGraphics() This method returnsGraphics2D(Graphics2D 类扩展 Graphics 类,以提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制。它是用于在 Java(tm) 平台上呈现二维形状、文本和图像的基础类。)
jquery related Api:post request asynchronous update
Achieve results
The prime Minister is a first-off HTML code:
Next is the servlet that generates the verification code on the server side
Package Gqx.shoppingcar.testservlet;import Java.awt.color;import Java.awt.font;import java.awt.Graphics;import Java.awt.image.bufferedimage;import Java.io.ioexception;import Java.util.random;import Javax.imageio.ImageIO; Import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Javax.servlet.http.httpsession;public class Authimageservlet extends HttpServlet {private static final String Content_ TYPE = "text/html; charset=gb2312 "; Set the size of the letters, size private font mfont = new Font ("Times New Roman", Font.plain, 24); public void Init () throws Servletexception {Super.init (); }//Generate RGB color getrandcolor (int fc,int BC) within the specified range {Random random = new random (); if (fc>255) fc=255; if (bc>255) bc=255; int R=fc+random.nextint (BC-FC); int G=fc+random.nextint (BC-FC); int B=fc+random.nextint (BC-FC); return new Color (R,G,B); public void Service (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexc Eption {//no-cache indicates that the request or response message cannot be cached Response.setheader ("Pragma", "No-cache"); Response.setheader ("Cache-control", "No-cache"); Response.setdateheader ("Expires", 0); Indicates that the generated response is a picture Response.setcontenttype ("Image/jpeg"); int width=100, height=25; Create a picture buffer bufferedimage image = new BufferedImage (width, height, bufferedimage.type_int_rgb); Get picture Processing object Graphics g = Image.getgraphics (); Fill background color random random = new random (); G.setcolor (Getrandcolor (200,250)); G.fillrect (1, 1, width-1, height-1); Sets the border color G.setcolor (new color (102,102,102)); G.drawrect (0, 0, width-1, height-1); Draw random line for (int i=0;i<15;i++) {int x = Random.nextint (width-1); int y = random.nextint (height-1); int xl = Random.nextint (6) + 1; int yl = Random.nextint (12) + 1; G.drawline (x,y,x + xl,y + yl); }//Draw random lines from the other side for (int i = 0;i < 10;i++) {int x = Random.nexti NT (WIDTH-1); int y = random.nextint (height-1); int xl = Random.nextint (12) + 1; int yl = Random.nextint (6) + 1; G.drawline (X,y,x-xl,y-yl); }//Write Text G.setfont (Mfont); Generates a random number and converts the random number to the letter String srand= ""; for (int i=0;i<4;i++) {int itmp = Random.nextint (26) + 65; Char ctmp = (char) itmp; SRand + = string.valueof (ctmp); G.sEtcolor (New Color (20+random.nextint, 20+random.nextint), 20+random.nextint (110)); Draws the text given by the specified string using the current font and color of this graphics context. void drawstring (string str, int x,int y) str-string to draw. X-x coordinates. Y-y coordinates g.drawstring (string.valueof (ctmp), 22*i+10,22); } HttpSession session = Request.getsession (true); Session.setattribute ("Rand", SRand); G.dispose (); Writes an image to the outputstream using any imagewriter that supports a fixed type. Imageio.write (Image, "JPEG", Response.getoutputstream ()); } } Data is sent on a response page, and the results are returned to the display layer
Package Gqx.shoppingcar.testservlet;import Java.io.ioexception;import Java.io.printwriter;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Javax.servlet.http.httpsession;public class Codecheck extends HttpServlet {public void doget (httpservletrequest Request, HttpServletResponse response) throws Servletexception, IOException {String code=request.getparameter ("code") ; String result= ""; HttpSession session=request.getsession (); SYSTEM.OUT.PRINTLN (code); String value= (String) session.getattribute ("Rand"), if (!code.equals (value)) {result= "<font color= ' red ' > Captcha error </font> "; SYSTEM.OUT.PRINTLN ("Error");} else {} response.setcontenttype ("Text/html;charset=utf-8"); SYSTEM.OUT.PRINTLN (result); Response.getwriter (). Print (result);} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { This.doget (Request, response);}} The test of the name is similar
Package Gqx.shoppingcar.testservlet;import Gqx.shoppingcar.service.impl.seroperate;import java.io.IOException; Import Java.io.printwriter;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class ValiateUserName Extends HttpServlet {/** * handles Ajax to verify that the name already exists */private static final long serialversionuid = 1l;public void Doget (Httpservle Trequest request, HttpServletResponse response) throws Servletexception, IOException {String name=request.getparameter ("UserName"); Seroperate operate =new seroperate (); String result=null;if (operate.exist (name)) {result= "<font color= ' Red ' > The user name has been used </font>";} else {result= "";} Response.setcontenttype ("Text/html;charset=utf-8"); Response.getwriter (). Print (result);} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { This.doget (request, Response);}}The seroperate here is the interface implementation class that I handle in the service layer of MVC. Do not write, will be in the back of the shopping cart code to write all up.
Java Verification Code and Ajax judgment