Verification code Making (i): using servlet to implement verification code

Source: Internet
Author: User

Using Servlets to implement verification codesLet's take a look at why verification codes are used in project development, and what are the benefits of using verification codes for our projects?1, if the development does not use the verification code will bring those harm to the project:①, if the user constantly login, you can crack the user password②, create an account for a site③, submitting spam data to a website④, a series of damage to a website swipe tickets2, on the Internet can be seen in fact the verification code is through the dynamic acquisition of images and the content of the picture constantly changing process, let us say that the generation of verification code used in those classes, such as:1. BufferedImage Image Data Buffer2, graphics drawing pictures3. Color gets colors4. Random Number acquisition5. ImageIO Output Imageknowing the implementation class used to generate the verification code, let's define our own servlet class to implement the verification code, with the following steps:
1. First create a page: index.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
2, the following to create its
Package Com.code;import Java.awt.color;import;import java.awt.image.bufferedimage;import;import;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;public class ImageServlet Extends HttpServlet {public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {bufferedimage bimage=new bufferedimage (n., bufferedimage.type_int_rgb);// Drawing graphics Graphics=bimage.getgraphics (); Color Color=new color (220,150,130); Graphics.setcolor (color);//Draw a frame for the picture graphics.fillrect (0, 0, 78, 27);/** * content displayed in the picture * Combination of letters and numbers */char[] ch= "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789". ToCharArray ();//randomly get inside contents random random=new random (); int len=ch.length,index; StringBuffer sb=new StringBuffer (); for (int i=0;i<4;i++) {index=random.nextint (len); graphics. SetColor (New Color (Random.nextint), Random.nextint (188), Random.nextint (255)), graphics.drawstring (ch[index]+ "", (i*15) +3, Sb.append (Ch[index]);} Save the CAPTCHA to the session for later comparisons using Request.getsession (). SetAttribute ("Piccode", Sb.tostring ()), Imageio.write (Bimage, "JPG", Response.getoutputstream ());} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {}}
After you finish writing the Imageservlet class, you need to configure the servlet in Web. xml
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://" Xmlns:xsi= "Http://" xsi:schemalocation= "Http:// http :// ">  <servlet>    <servlet-name>imageservlet </servlet-name>    <servlet-class>com.code.ImageServlet</servlet-class>  </servlet >  <servlet-mapping>    <servlet-name>ImageServlet</servlet-name>    <url-pattern >/servlet/ImageServlet</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

After the above steps are completed, deploying the Web project to Tomcat runs on it, running the results such as:
This is a simple application of the servlet to implement the verification Code case, it is not easy to do it! In the future, more convenient, more concise, more functional methods to implement the verification code will be updated.

Verification code Making (i): using servlet to implement 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: 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.