This demo code consists mainly of the following three parts:
1.checkcode.java: Used to generate verification code
2.checkCodeServler
3.check.jsp Verification
Here's what the Checkcode.java:
Copy Code code as follows:
Used to get four-bit random numbers
Private char maptable[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '};
Generates a CAPTCHA and returns a randomly generated number
Public String getensure (int width, int height, outputstream os) {
if (width <= 0)
width = 60;
if (height <= 0)
height = 20;
BufferedImage image = new BufferedImage (width, height,bufferedimage.type_int_rgb);
Get Graphics context
Graphics g = image.getgraphics ();
Set Background color
G.setcolor (New Color (0xDCCCCC));
G.fillrect (0, 0, width, height);
Draw a border
G.setcolor (Color.Black);
G.drawrect (0, 0, width-1, height-1);
Take a randomly generated authentication code
String strensure = "";
4 represents 4-bit captcha
for (int i = 0; i < 4; ++i) {
Strensure + = maptable[(int) (Maptable.length * Math.random ())];
}
Display the authentication code in the image
G.setcolor (color.red);
G.setfont (New Font ("Atlantic Inline", Font.plain, 14));
The exact coordinates of the painting
String str = strensure.substring (0, 1);
g.DrawString (str, 8, 14);
str = strensure.substring (1, 2);
g.DrawString (str, 20, 15);
str = strensure.substring (2, 3);
g.DrawString (str, 35, 18);
str = strensure.substring (3, 4);
g.DrawString (str, 45, 15);
Releasing the graphics context
G.dispose ();
try{
Output image to Page
Imageio.write (Image, "JPEG", OS);
catch (IOException e) {
Return "";
}
return strensure; Returns the generated random number
}
And again, the Checkcodeservlet content.
Copy Code code as follows:
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
DoPost (request, response);
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Disable caching, each time you access this page, regenerate
Response.setheader ("Pragma", "No-cache");
Response.setheader ("Cache-control", "No-cache");
Response.setdateheader ("Expires", 0);
To generate an instance object for a validation code
Checkcode ie = new Checkcode ();
The method inside the call returns the string in the generated verification code
String str = ie.getensure (0,0,response.getoutputstream ());
Gets the session and saves the string in session, making the basis for the back contrast
HttpSession session = Request.getsession ();
Session.setattribute ("Strensure", str);
}
And then Web.xml's configuration of the servlet
Copy Code code as follows:
<servlet>
<servlet-name>CheckServlet</servlet-name>
<servlet-class>com.blog.servlet.CheckServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CheckServlet</servlet-name>
<url-pattern>/check</url-pattern>
</servlet-mapping>
Finally, a reference to the JSP page
Copy Code code as follows:
<title> Verification Code </title>
<script type= "Text/javascript" language= "JavaScript" >
//re-obtain validation characters
function changeimage ()
{
Click to trigger the picture overload event to complete the replacement of the picture verification Code
document.getElementById ("Imgrandom"). src = document.getElementById ("Imgrandom"). src + '? ';
}
</script>
<body>
&nbs p;
<a href= " Javascript:changeimage (); " > See?</a>
</body>
In the JSP page, just point the img src attribute to the servlet that generated the captcha, and point to the URL of the Servle map in WEB.XMLT.