JSP implementation of the login function add verification code _jsp programming

Source: Internet
Author: User
Tags gettext int size

JSP login verification, Web login verification with verification Code checksum, login function to add verification code

Part_1: Class specifically used to generate a CAPTCHA picture: 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 * @function: This is the class used to test randomly generated captcha images;/public class Verificationcode {/** * unit test 
    , try to automatically generate the verification code picture////This function is used in unit testing, here private outside of the call can not be; * @Test/* Public */private void Test_fun () { 
    Verificationcode VC = new Verificationcode (); 
    BufferedImage image = Vc.getimage (); try {//Generate a CAPTCHA picture and save to the specified path Verificationcode.output (image, New FileOutputStream (". \\imag 
    E\\vcode_2.jpg ")); 
    catch (FileNotFoundException e) {e.printstacktrace (); ///Output randomly generated text content to the console for verifying System.out.println (Vc.gettEXT ()); private int w = 70;//wide private int h = 35;//High private String text;//text content (authentication code string) Private Random R = new R 
  Andom (); 
  Private string[] FontNames = {"XXFarEastFont-Arial", "XXFarEastFont-Arial", "bold", "Microsoft Ya-hei", "italics _gb2312"}; The random character set does not include 0 and o,o,1 and L, because these are not easily distinguishable from private String codes = "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYXZ 
  Background color of the CAPTCHA Picture: white private color bgcolor = new color (255, 255, 255); /** * Returns a CAPTCHA image buffer object: BufferedImage */Public BufferedImage getImage () {BufferedImage image = Createima 
    GE (); 
    Gets the drawing environment (brush tool) graphics2d g2 = (graphics2d) image.getgraphics (); 
    SB: Used to save Authenticode string text content StringBuilder SB = new StringBuilder (); 
      for (int i = 0; i < 4; ++i) {//randomly generate 4 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 Authenticode text content DRAWline (image);//Draw interference line return image; 
  /** * @return Get Authenticode text content */public String GetText () {return text; /** * @param image * @param out * Writes the text to the specified output stream. 
      For example, in this test FileOutputStream specified save path/public static void output (BufferedImage image, outputstream out) {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 line 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_ 
    graphics2d g2 = (graphics2d) image.getgraphics (); 
    G2.setcolor (This.bgcolor); 
    G2.fillrect (0, 0, W, h); 
  return image; } 

part_2: Login interface: login.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <% String Path = Request.getcontextpath 
  (); String basepath = request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () + PA 
th + "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  

part_3: Servlet:LoginVerificationServlet.java

for processing login checksums

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 =-35209946753 
    public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { 
    1. Create a new Verificationcode class; Verificationcode VC = new Verificationcode (); 
    2. Obtaining BufferedImage objects from the Verificationcode class; BufferedImage bufimage = Vc.getimage (); 
    3. Obtain the text content in the verification code at the same time, and put it into the session field for verification; String Code_text = Vc.gettext (); 
    Request.getsession (). setattribute ("Code_text", Code_text); 4. Output the generated picture to the client browser verificationcode.output (Bufimage, Response.getoutputstream ());
  }//end Method-doget public void DoPost (HttpServletRequest request, httpservletresponse response) throws Serv 
  Letexception, IOException {//do same as get-method:doget (request, response);  }//End Method-dopost}

part_4: Prompt interface After successful login 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 () + PA 
th + "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  

part_5: Prompt interface After successful login 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 () + PA 
th + "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  

The above is a small set to introduce the JSP implementation of the login function to add verification code, we hope to help, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.