Java implementation Verification code making one of your own hands

Source: Internet
Author: User

Before the verification code are now looking for the current use, the following is their own follow-up code to fix, record, share to everyone.

I'm using a servlet here, and the servlet code is as follows

Import Java.awt.Color;
Import Java.awt.Graphics;
Import Java.awt.image.BufferedImage;
Import java.io.IOException;
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 {

@Override
protected void Doget (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
BufferedImage bi=new bufferedimage (68,22, BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics G=bi.getgraphics ();
Color C=new color (200,150,255);
G.setcolor (c);
G.fillrect (0, 0, 68, 22);//Wide Height

Set the character of the Verification code
Char[] ch= "QWERTYUIOPASDFGHJKLZXCVBNM0123456789". ToCharArray ();
Random r=new random ();
int len=ch.length,index;
StringBuffer sb=new StringBuffer ();

for (int i=0;i<4;i++) {
Index=r.nextint (len);
Set each font to a different color
G.setcolor (New Color (R.nextint), R.nextint (188), R.nextint (255));
g.DrawString (ch[index]+ "", (i*15) +3,18);//Drawing
Sb.append (Ch[index]);//Record Verification code
}
Req.getsession (). SetAttribute ("Piccode", sb.tostring ());//Save to session scope
Imageio.write (BI, "JPG", Resp.getoutputstream ()); Write to the corresponding page
Super.doget (req, resp);
}

@Override
protected void DoPost (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
Doget (req, resp);
}
}

In the servlet above, each generated verification code is saved in the session.

Req.getsession (). SetAttribute ("Piccode", sb.tostring ());

When the user submits, we can go through

String code=req.getsession (). getattribute ("Piccode");

To obtain the generated verification code and user-filled verification code to compare

Configuration in Web. xml

<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>com.test.servlet.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/image.do</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

Index.jsp page Code

<form action= "<%=request.getcontextpath ()%>/login.do" method= "Get" >
<center>
<div>
CAPTCHA: <input type= "text" name= "Code1"/>
/image.do" id= "Code1" onclick= "Relodcode (This, ' image.do ') ) "/>
</div>
<input type= "Submit" value= "Submission"/>
</center>
</form>
<script type= "Text/javascript" >
function Relodcode (obj,url) {
var time=new Date ();
Obj.src= "<%=request.getcontextpath ()%>/" +url+ "? time=" +time;
}
</script>

(Click Verification Code Refresh)

Described above is to write their own, but we generally use to use the verification code when the general will use some plug-ins, the next article to introduce the use of Kaptcha verification code

Java implementation Verification code making one of your own hands

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.