This example demonstrates the use of a verification code in the user login page, which is generated using Javabena and Servlets as follows:
Makecertpic.java
Package cert;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.image.BufferedImage;
Import java.io.IOException;
Import Java.io.OutputStream;
Import Java.util.Random;
Import Javax.imageio.ImageIO;
/**
*
* @author welins Generate verification code picture
*/
public class Makecertpic {
A character set that can appear in a captcha picture, which can be modified as needed
Private char maptable[] = {' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' 0 ', ' 2 ', ' 3 ',
' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E '};
/**
* Function: Generate color verification Code picture parameter width for the resulting picture, the height of the parameter is generated, the parameter OS is the output stream of the page
*
*/
Public String getcertpic (int width, int height, outputstream os) {
if (width <= 0) {
width = 60;
}
if (height <= 0) {
height = 20;
}
/**
* BufferedImage is a subclass of image, BufferedImage generated picture has an image buffer in memory,
* Using this buffer we can easily operate this image, usually used to do picture modification operations such as size change, image gray, set the picture transparent or opaque.
* (500,500,BUFFEREDIMAGE.TYPE_INT_RGB) What does it mean? BufferedImage (int width,
* int height, int imageType) Width:width of the created image height
*: Height of the created image Imagetype:type of the created image
*
* Bufferedimage.type_int_rgb: Represents an image that has an integer pixel of 8-bit RGB color
*/
BufferedImage image = new BufferedImage (width, height,
BUFFEREDIMAGE.TYPE_INT_RGB);
/**
* Get the context of the graph to create a brush g
*/
Graphics g = image.getgraphics ();
Set Background color
G.setcolor (New Color (0xDCDCDC));
Starting x coordinate, starting y-coordinate, picture broadband, picture height
G.fillrect (0, 0, width, height);
Draw Border
G.setcolor (Color.Black);
G.drawrect (0, 0, width-1, height-1);
Randomly generated verification codes
String strensure = "";
4 code generates 4-bit verification code, if you want to generate more bits of the verification code, then increase the value
for (int i = 0; i < 4; i++) {
Randomly get the subscript in the Maptable.length*math.random (), and the array maptable the value out by subscript.
Strensure = Strensure
+ maptable[(int) (Maptable.length * Math.random ())];
}
The verification code is realistic to the image, if you want to generate more bits of the verification code, add drawstring statement
G.setcolor (Color.Black);
G.setfont (New Font ("Atlantic Inline", Font.plain, 18));
String str = strensure.substring (0, 1);
g.DrawString (str, 8, 17);
str = strensure.substring (1, 2);
g.DrawString (str, 20, 15);
str = strensure.substring (2, 3);
g.DrawString (str, 30, 16);
str = strensure.substring (3, 4);
g.DrawString (str, 45, 17);
Randomly generates 10 interference points,
Random rand = new Random ();
for (int i = 0; i < i++) {
int x = rand.nextint (width);
int y = rand.nextint (height);
G.drawoval (x, Y, 2, 4);
}
Releasing the graphics context
G.dispose ();
try {
Output image to Page
Imageio.write (Image, "JPEG", OS);
} catch (IOException e) {
Return "";
}
return strensure;
}
}
Showcertpic.java
Package cert;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Showcertpic extends HttpServlet {
@Override
protected void Service (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
TODO auto-generated Method Stub
Super.service (req, resp);
Makecertpic image = new Makecertpic ();
String str = image.getcertpic (0, 0, Resp.getoutputstream ());
Save verification Code to session
Req.getsession (). SetAttribute ("CertCode2", str);
}
}
The Web. xml file is configured as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">
<display-name></display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ShowCertPic</servlet-name>
<servlet-class>cert. Showcertpic</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowCertPic</servlet-name>
<url-pattern>/ShowCertPic</url-pattern>
</servlet-mapping>
</web-app>
Login page, 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" >
<base href= "<%=basePath%>" >
<title> Login Page </title>
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This is my page" >
<!--
<link rel= "stylesheet" type= "Text/css" href= "Styles.css" >
-
<body>
<form action= "logincheck.jsp" method= "POST" >
<table align= "center" >
<TR align= "center" >
<td> System Login </td>
</tr>
<tr>
<td> Username: <input type= "text" name= "username" >
</td>
</tr>
<tr>
<td> Password: <input type= "password" name= "password" >
</td>
</tr>
<tr>
<td> captcha: <input type= "text" name= "Certcode" >Id= "Certcode" src= "Showcertpic" alt= "Verification Code" onclick= "this.src= ' Showcertpic '" >
</td>
</tr>
<tr align= "Left" >
<td><input type= "Submit" value= "Submission" >
</td>
</tr>
</table>
</form>
</body>
Login Verification Code Simple processing:
<%@ 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" >
<base href= "<%=basePath%>" >
<title> Login Verification Page </title>
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This is my page" >
<!--
<link rel= "stylesheet" type= "Text/css" href= "Styles.css" >
-
<body>
<%
String Certcode = Request.getparameter ("Certcode");
String certCode2 = (string) session.getattribute ("CertCode2");
if (Certcode.equals (CertCode2)) {
Out.print ("Verification code is correct");
} else {
Out.print ("Captcha error");
}
%>
</body>
Over, where does the problem arise, please the great God?
Using a CAPTCHA in a JSP