Java Login Verification Code

Source: Internet
Author: User
Tags border color getcolor set background

First, create a Web project

Use simple servlet to demonstrate Java captcha image generation, servlet simple to use reference http://www.cnblogs.com/ywlaker/p/6038676.html

Second, generate verification code

Create Vcodeobject.java, store verification codes and pictures

Package Com.demo.vcode;import Java.io.inputstream;public class Vcodeobject {private String code;private inputstream in; Public Vcodeobject () {}public-Vcodeobject (String code, InputStream in) {This.code = Code;this.in = in;} Getters & Setters}

Create a Vcode.java to generate a captcha and a picture

Package Com.demo.vcode;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.io.ioexception;import Java.io.inputstream;import Java.util.random;import Javax.imageio.ImageIO;public class Vcode {public Vcodeobject Create () {//Generate random verification code String code = This.generaterandomverifycode (4);//Generate picture with Captcha BufferedImage img = This.generateimgwithcode (code, InputStream); This.putimginstream (IMG); return new Vcodeobject (code, in);} /** * Randomly generated verification code * * @param length * @return string */private string generaterandomverifycode (int length) {//The length of the verification code that needs to be generated if ( Length <= 0 | | Length >= 8) length = 4;//The range of randomly generated characters string scope = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; StringBuffer code = new StringBuffer (); Random random = new random (), for (int i = 0; i < length; i++) {Code.append (Scope.charat (Random.nextint (62))} return code.tostring ();} /** * Generate a picture with a verification code * * @param code * @param width * @param height * @return bufferedimage */private bufferedimage Generateimgwit Hcode (String code, int width, int height) {bufferedimage img = new BufferedImage (width, Height,bufferedimage.type_int_rgb ); Graphics brush = Img.getgraphics ();//Set Background and border color Brush.setcolor (This.getcolor ()); Brush.fillrect (0, 0, width-1, height-1 ); brush.drawrect (0, 0, width-1, height-1);//write Verification Code Brush.setcolor (This.getcolor ()); Brush.setfont ("Times New R" Oman ", Font.Italic,"); brush.drawstring (Code, 7, 22);//Draw the interfering line//Picture finish, close the resource Brush.dispose (); return img;}  /** * Get random color * * @return color */private color GetColor () {Random random = new Random (); int r = random.nextint (255); int g = Random.nextint (255); int b = Random.nextint (255); return new Color (R, G, b);} /** * Put the picture into the input stream * * @param image * @return InputStream */private inputstream putimginstream (bufferedimage image) {Bytearr Ayoutputstream out = new Bytearrayoutputstream (); try {imageio.write (image, "JPEG", Out);} catch (IOException e) {e.printstacktrace ();} InputStream in = new Bytearrayinputstream (Out.tobytearray ()), return in;}}
Third, the output verification code

Create Demoservlet.java

Package Com.demo.vcode;import Java.io.ioexception;import Java.io.inputstream;import java.io.outputstream;import Javax.servlet.servletconfig;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class DemoServlet Extends HttpServlet {private static final long serialversionuid = 1l;public void init (servletconfig config) throws Servlet Exception {}public void doget (httpservletrequest req, httpservletresponse Res) throws IOException, Servletexception { Res.setcontenttype ("Image/png"); Vcode Vcode = new Vcode (); Vcodeobject vcodeobject = Vcode.create (); InputStream in = Vcodeobject.getin (); Req.getsession (). SetAttribute ("Vcode", Vcodeobject.getcode ()); byte[] bytes = new Byte[1024];int length = 0;try {OutputStream out = Res.getoutputstream (); while ( Length = In.read (bytes))! =-1) {out.write (bytes, 0, length);} In.close (); Out.flush ();} catch (Exception e) {}}public void DoPost (HttpServletRequest req, HttpServletResponse resp) throws IOException, servletexception {}public void Destroy () {}} 

Configure Web. xml

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version= " 2.5 "><display-name>vcode</display-name><servlet><servlet-name>vcode</ servlet-name><servlet-class>com.demo.vcode.demoservlet</servlet-class></servlet>< Servlet-mapping><servlet-name>vcode</servlet-name><url-pattern>/vcode</url-pattern> </servlet-mapping></web-app>

Browser Address bar Access

Http://127.0.0.1:8080/vcode/vcode

Java Login Verification Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.