JSP adds a verification code to implement the login function, jsp implements the login verification code

Source: Internet
Author: User

JSP adds a verification code to implement the login function, jsp implements the login verification code

Jsp login verification, webpage login verification with verification code, login function to add Verification Code

Part_1: class used to generate a verification code image: VerificationCode. Java

Package cn. mike. javase. test; import java. awt. color; import java. awt. font; import java. awt. graphics2D; import java. awt. image. bufferedImage; import java. io. file; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. outputStream; import java. util. random; import javax. imageio. imageIO; import org. junit. test;/*** @ author: Administrator * @ fu Nction: This is the class used to test the randomly generated Verification Code image; */public class VerificationCode {/*** unit test, try to automatically generate a verification code image * // this function is used for unit testing. Here, it cannot be called outside the private domain; /* @ Test * // * public */private void test_fun () {VerificationCode vc = new VerificationCode (); BufferedImage image = vc. getImage (); try {// generate the verification code image and save it to the specified path VerificationCode. output (image, new FileOutputStream (new File (". \ image \ vcode_2.jpg ");} catch (FileNotFou NdException e) {e. printStackTrace ();} // outputs randomly generated text content to the console for System verification. out. println (vc. getText ();} private int w = 70; // wide private int h = 35; // high private String text; // text content (Verification code String) private Random r = new Random (); private String [] fontNames = {"", "文 ", ""," ", "_ GB2312"}; // random character sets do not include 0, o, 1, and l, because these cannot distinguish private String codes = "23456789 abcdefghijkmnopqrstuvwxyzabcdefghilmjknopqrs TUVWXYXZ "; // background Color of the Verification Code image: White private Color bgColor = new Color (255,255,255);/*** return a verification code image buffer object: BufferedImage */public BufferedImage getImage () {BufferedImage image = createImage (); // obtain the drawing environment (paint brush tool) Graphics2D g2 = (Graphics2D) image. getGraphics (); // sb: used to save the text content of the verification code string StringBuilder sb = new StringBuilder (); for (int I = 0; I <4; ++ I) {// Four Characters 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 (); // record the text content of the Verification Code drawLine (image); // draw the interference line return image ;} /*** @ return get verification code text content */public String getText () {return text ;} /*** @ param image * @ param out * writes the text to the specified output stream. For example, the storage path */public static void output (BufferedImage image, OutputStream out) specified by FileOutputStream in this test {try {ImageIO. write (image, "jpeg", out);} catch (IOException e) {e. printStackTrace () ;}} private void drawLine (BufferedImage image) {Graphics2D g2 = (Graphics2D) image. getGraphics (); for (int I = 0; I <3; ++ I) {// draw 3 interference lines int x1 = r. nextInt (w); int y1 = r. nextInt (h); int x2 = r. nextInt (w); int y2 = r. nextInt (h); g2.setColor (Color. BLUE); g2.drawLine (x1, y1, x2, y2) ;}} private Color randomColor () {int red = r. nextInt (150); int green = r. nextInt (150); int blue = r. nextInt (150); 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 (5) + 24; return new Font (fontName, style, size);} 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 ;}}

Part_2: logon interface: Login. jsp

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

Part_3: servlet for processing login verification: LoginVerificationServlet. java

Package cn. mike. servlet. test_1212; import java. awt. image. bufferedImage; import java. io. IOException; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import cn. mike. javase. test. verificationCode; public class GetVerificationCodeServlet extends HttpServlet {private static final long serialVersionUID =-response; public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 1. create a VerificationCode class; VerificationCode vc = new VerificationCode (); // 2. obtain the BufferedImage object from the VerificationCode class; BufferedImage bufImage = vc. getImage (); // 3. obtain the text content in the verification code and put it in the session domain for verification. String code_text = vc. getText (); request. getSession (). setAttribute ("code_text", code_text); // 4. output the generated image to the client browser VerificationCode. output (bufImage, response. getOutputStream ();} // end method-doGet public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// do same as GET-method: doGet (request, request, response);} // end method-doPost}

Part_4: successful login after the prompt interface 1: success-page-1.jsp

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

Part_5: successful login after the prompt interface 1: success-page-2.jsp

<% @ Page language = "java" import = "java. util. date "pageEncoding =" UTF-8 "%> <% @ page language =" java "import =" java. text. simpleDateFormat "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

The above section describes how to add a verification code for JSP login. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

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.