The example of this article describes the method of Jsp+servlet programming to implement the verification code. Share to everyone for your reference, specific as follows:
Here are two classes, one for the authentication code implementation, and one for the background servlet to verify that the input is correct:
Codeutil.java--------Verification Code is specifically implemented:
Package util;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.image.BufferedImage;
Import java.io.IOException;
Import Java.io.OutputStream;
Import Java.io.PrintWriter;
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;
Import Javax.servlet.jsp.PageContext;
Import javax.servlet.jsp.tagext.BodyContent;
Import Com.sun.image.codec.jpeg.JPEGCodec;
Import Com.sun.image.codec.jpeg.JPEGImageDecoder;
Import Com.sun.image.codec.jpeg.JPEGImageEncoder; public class Codeutil extends HttpServlet {public void service (HttpServletRequest request, httpservletresponse response
) throws Servletexception, IOException {//system.out.println ("hahaha");
Response.setcontenttype ("Image/jpeg"); Set page does not cache Response.setheader ("PragMa "," No-cache ");
Response.setheader ("Cache-control", "No-cache");
Response.setdateheader ("Expires", 0);
Create an image in memory int width = 90;
int height = 35;
BufferedImage image = new BufferedImage (WIDTH,HEIGHT,BUFFEREDIMAGE.TYPE_INT_RGB);
Gets the graphics context Graphics g = image.getgraphics ();
Random class Random Random = new Random ();
Set Background G.setcolor (Getrandcolor (200, 250));
G.fillrect (0, 0, width, height);
Set font g.setfont (new Font ("Times New Roman", font.plain,30);
Randomly generated disturbing line G.setcolor (Getrandcolor (160, 200));
for (int i = 0; i < i++) {int x = random.nextint (width);
int y = random.nextint (height);
int xl = Random.nextint (12);
int yl = Random.nextint (12);
G.drawline (x, y, X + xl, y + yl); ///Randomly generate 4-bit captcha string[] codes = {"0", "1", "2", "3", "4", "5", "" 6 "," 7 "," 8 "," 9 "," A "," B "," C "," D "," E "," F "," G "," H "," I "," J "," K " , "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M "," N "," O "," P "," Q "," R "," s "," T "," U "," V "," w "," X "," Y "," Z "};
String code = "";
for (int i=0;i<4;i++) {String str = codes[random.nextint (codes.length)];
Code + + str;
Display the authentication code to the image G.setcolor (new Color (+ Random.nextint (), + random.nextint (110)); Call functions come out of the same color, probably because the seeds are too close, so only directly generated g.drawstring (str, * i +13, 27);
Text spacing *i+ distance from left margin, top margin} HttpSession session=request.getsession ();
The authentication code is stored in the session session.setattribute ("code", code);
Image entry into force g.dispose ();
Output image to page imageio.write (image, "JPEG", Response.getoutputstream ()); Add the following code, the runtime will not appear Java.lang.IllegalStateException:getOutputStream () has already been called ...
such as abnormal response.getoutputstream (). Flush ();
Response.getoutputstream (). Close ();
Response.flushbuffer ();
//Get random color private color getrandcolor (int fc,int BC) {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);
}
}
Checkcodeutil.java-----------User Input validation
Package util;
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 Checkcodeutil extends HttpServlet {public void service (HttpServletRequest request, HttpServletResponse Res
Ponse) throws Servletexception, IOException {request.setcharacterencoding ("utf-8");
Response.setcontenttype ("Text/html;charset=utf-8");
PrintWriter out = Response.getwriter ();
Obtain the verification code from session HttpSession session=request.getsession ();
String Code=session.getattribute ("code"). toString ();
SYSTEM.OUT.PRINTLN (code);
Gets the user input verification code String input=request.getparameter ("code");
SYSTEM.OUT.PRINTLN (input);
if (code.equalsignorecase (input)) {//Forwarding data Request.setattribute ("Result", "true"); Request.getrequestdispatcher ("test/regist.jsp"). Forward (Request, response);
Response.sendredirect (Request.getcontextpath () + "/regist/regist.jsp");
}else{Request.setattribute ("Result", "false");
Request.getrequestdispatcher ("test/regist.jsp"). Forward (request, response);
}
}
}
To test the page effect:
To test the page code:
<%@ page session= "true" pageencoding= "Utf-8 contenttype=" text/html; Charset=utf-8 "%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
I hope this article will help you with JSP programming.