jsp generates the captcha code

  • 2020-05-10 18:37:44
  • OfStack

Call method: the image tag on jsp page can be directly called as follows is the tag code
< img border=0 src="image.jsp" > , just post the code in the area where the captcha will be displayed.)
< %@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" % >
< %!
public static String code="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Color getRandColor(int fc,int bc){// get random colors for a given range
Random random = new Random();
if(fc > 255) fc=255;
if(bc > 255) bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
% >
< %
// the Settings page is not cached
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
// create the image in memory and set the display size of the image
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// gets the graphics context
Graphics g = image.getGraphics();
// generates a random class
Random random = new Random();
// set the background color
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
// set the font
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
/ / picture frame
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);
// randomly generated 155 interference lines, so that the authentication code in the image is not easily detected by other programs
g.setColor(getRandColor(160,200));
for (int i=0;i < 155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
// take the randomly generated authentication code (made up of Numbers and letters)
String sRand="";
for (int i=0;i < 4;i++){
int rand=random.nextInt(62);
sRand+=String.valueOf(code.charAt(rand));
// displays the authentication code in the image
g. setColor (new Color (20 + random. nextInt (110), 20 + random. nextInt (110), 20 + random. nextInt (110))); // the color of the calling function is the same, probably because the seed is too close, so it can only be generated directly
g.drawString(String.valueOf(code.charAt(rand)),13*i+6,16);
}
// store the authentication code in SESSION
session.setAttribute("rand",sRand);
// image in effect
g.dispose();
// output image to page
ImageIO.write(image, "JPEG", response.getOutputStream());
% >
Below is an test.jsp to test captcha generation and then pass it to check.jsp for processing
< %@ page contentType="text/html;charset=gb2312" % >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< title > Authentication code input page < /title >
< meta http-equiv="Content-Type" content="text/html; charset=gb2312" >
< META HTTP-EQUIV="Pragma" CONTENT="no-cache" >
< META HTTP-EQUIV="Cache-Control" CONTENT="no-cache" >
< META HTTP-EQUIV="Expires" CONTENT="0" >
< /head >
< body >
< form method=post action="check.jsp" >
< table >
< tr >
< td align=left > The authentication code generated by the system: < /td >
< td > < img border=0 src="image.jsp" > < /td >
< /tr >
< tr >
< td align=left > Enter the authentication code above: < /td >
< td > < input type=text name=rand maxlength=4 value="" > < /td >
< /tr >
< tr >
< td colspan=2 align=center > < input type=submit value=" submit test" > < /td >
< /tr >
< /form >
< /body >
< /html >
Below is an check.jsp
< %@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" % >
< html >
< head >
< title > Authentication code verification page < /title >
< meta http-equiv="Content-Type" content="text/html; charset=gb2312" >
< META HTTP-EQUIV="Pragma" CONTENT="no-cache" >
< META HTTP-EQUIV="Cache-Control" CONTENT="no-cache" >
< META HTTP-EQUIV="Expires" CONTENT="0" >
< /head >
< body >
< %
String rand = (String)session.getAttribute("rand");
String input = request.getParameter("rand");
% >
The authentication code generated by the system is: < %= rand % > < br >
The authentication code you entered is: < %= input % > < br >
< br >
< %
if (rand.equals(input)) {
% >
< font color=green > Enter the same, authentication successful! < /font >
< %
} else {
% >
< font color=red > Enter different, authentication failed! < /font >
< %
}
% >
< /body >
< /html >

Related articles: