To implement a verification code in Java:
1. Create the entity class for the verification code as follows :
- Import Java.awt.BasicStroke;
- Import Java.awt.Color;
- Import Java.awt.Font;
- Import Java.awt.Graphics2D;
- Import Java.awt.image.BufferedImage;
- Import java.io.IOException;
- Import Java.io.OutputStream;
- Import Java.util.Random;
- Import Javax.imageio.ImageIO;
- public class Verifycode {
- /**
- * Nan accommodating heals scythe?
- * @author blemish lee Carpenter Å?
- */
- private int w = 50;
- private int h = 25;
- Private random r = new Random ();
- Private string[] fontnames = {"Yue ã 綋", "Å bond po 妤 bee 綋", "Hazel Contact 綋", "Atlas Jiang boss 呴 din Daeng", "妤 Bee 綋 _gb2312"};
- Private String codes = "23456789abcdefghjkmnpqrstuvwxyzABCEFGHIJKLMNPQRSTUVWXYZ";
- Private color bgColor = new Color (255,255,255);
- private String text;
- Private Color Randomcolor () {
- int red = r.nextint (200);
- int green = r.nextint (200);
- int blue = r.nextint (200);
- return new Color (Red,green,blue);
- }
- Private Font Randomfont () {
- int index = r.nextint (fontnames.length);
- String fontname = Fontnames[index];
- int style = r.nextint (4);
- int size = r.nextint (6) +15;
- return new Font (fontname,style,size);
- }
- private void DrawLine (BufferedImage image) {
- int num = 3;
- graphics2d g2 = (graphics2d) image.getgraphics ();
- for (int i=0;i<num;i++) {
- int x1 = r.nextint ();
- int y1 = r.nextint ();
- int x2 = r.nextint ();
- int y2 = r.nextint ();
- G2.setstroke (New Basicstroke (1.5F));
- G2.setcolor (Color.Blue);
- G2.drawline (X1,Y1,X2,Y2);
- }
- }
- Private char Randomchar () {
- int index = r.nextint (codes.length ());
- Return Codes.charat (index);
- }
- Private BufferedImage createimage () {
- BufferedImage image = new BufferedImage (W,H,BUFFEREDIMAGE.TYPE_INT_RGB);
- graphics2d g2 = (graphics2d) image.getgraphics ();
- G2.setcolor (This.bgcolor);
- G2.fillrect (0, 0, W, h);
- return image;
- }
- Public BufferedImage GetImage () {
- BufferedImage image = createimage ();
- graphics2d g2 = (graphics2d) image.getgraphics ();
- StringBuilder sb = new StringBuilder ();
- for (int i= 0;i<4;i++) {
- String s = Randomchar () + "";
- Sb.append (s);
- float x = I*1.0F*W/4;
- G2.setfont (Randomfont ());
- G2.setcolor (Randomcolor ());
- G2.drawstring (S, x, h-5);
- }
- This.text = sb.tostring ();
- DrawLine (image);
- return image;
- }
- Public String GetText () {
- return text;
- }
- public static void Output (BufferedImage image,outputstream out) throws ioexception{
- Imageio.write (Image, "JPEG", out);
- }
- }
2. Create a servlet for the verification code
- Import Java.awt.image.BufferedImage;
- Import java.io.IOException;
- Import Java.io.PrintWriter;
- Import Javax.imageio.ImageIO;
- Import javax.servlet.ServletException;
- Import Javax.servlet.annotation.WebServlet;
- Import Javax.servlet.http.HttpServlet;
- Import Javax.servlet.http.HttpServletRequest;
- Import Javax.servlet.http.HttpServletResponse;
- Import util. Verifycode;
- @WebServlet ("/verifycodeservlet")
- public class Verifycodeservlet extends HttpServlet {
- /**
- * Servlet
- * @author Weiqiang
- */
- public void doget (HttpServletRequest request, httpservletresponse response)
- Throws Servletexception, IOException {
- Verifycode VC = new Verifycode ();
- Response.setheader ("Cache-control", "No-cache");
- Response.setcontenttype ("Image/jpeg");
- BufferedImage bim = vc.getimage ();
- Imageio.write (BIM, "JPEG", Response.getoutputstream ());
- String Verifycode = vc.gettext ();
- Request.getsession (). SetAttribute ("Verifycode", Verifycode);
- }
- public void DoPost (HttpServletRequest request, httpservletresponse response)
- Throws Servletexception, IOException {
- Doget (request, response);
- }
- public void Destroy () {
- Super.destroy ();
- }
- public void Init () throws servletexception{
- }
- }
- 3. Configuring in XML
Servlet>
<servlet-name>VerifyCodeServlet</servlet-name>
<servlet-class>cn.com.shxt.servlet.VerifyCodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>VerifyCodeServlet</servlet-name>
<url-pattern>/VerifyCodeServlet</url-pattern>
</servlet-mapping>
3. Use the following code where you need to use the verification code:
[HTML]View PlainCopy
- <!--It's not clear what the Refresh function is.
- <script>
- function Changeverifycode () {
- document.getElementById ("Validatecodeimg"). src="verifycodeservlet?t=" +math.random ();
- }
- </Script>
- <!--This is the input box for entering the verification code--
- <INPUT id=txtsn size=4 name=txtsn>
- <!--This is the display of the captcha, shown in pictures--
- <!--Src's road strength is the servlet's way--
- <img src= "<%=basepath%>verifycodeservlet" border=0 id= "validatecodeimg" style= "height: 25px; width: 57px" span class= "tag" >/><a href= "#" onclick= " Changeverifycode () "> can't see clearly? Change a </a></ td>
4. The actual effect is as follows
String code = request.getparameter ("code");
String verifycode = (string) request.getsession (). getattribute ("Verifycode");
else if (!code.equals (Verifycode)) {//Login failed
Request.setattribute ("message", "Captcha error");
Request.getrequestdispatcher ("/login.jsp"). Forward (request, response);
The code of the foreground and the verification code in the session are compared to realize the function of verification code.
Java Implementation Verification Code login