Home > Back-end >  JSP dynamic Graphics captcha images on the page shows the code
JSP dynamic Graphics captcha images on the page shows the code

Time:11-30

<% @ page import="Java. The awt. Image. The BufferedImage" % & gt;
<% @ page import="Java. The awt. *" % & gt;
<% @ page import="Java. Util. Random" % & gt;
<% @ page import="javax.mail. Imageio. Imageio" % & gt;
<% @ page language="Java" pageEncoding="utf-8" % & gt;
<% @ page contentType="text/HTML. Charset=utf-8 "% & gt;



Title

<body>
<%

//in memory to create image
Response. SetHeader (" cache-control ", "no - Cache");
Int width=60, height=20;
BufferedImage image=new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB);

//get the brush
Graphics g=image. GetGraphics ();

//set the background color in
G.s etColor (Color BLACK);
G.f illRect (0, 0, width, height);

//take random authentication code (4 digits)
The Random RND=new Random ();
Int randNum=RND. NextInt + 1000 (8999);
String randStr=String. The valueOf (randNum);

//will be credited to the session authentication code
Session. SetAttribute (" randStr randStr);

//will display verification code to the image
G.s etColor (Color BLACK);
The Font Font=new Font (" ", the Font. PLAIN, 12);
G.s etFont (font);
G.d rawString (randStr, 10 or 17);
for(int i=0; i<100; I++) {
Int x=RND. NextInt (width);
Int y=RND. NextInt (height);
G.d rawOval (x, y, 1, 1);
}

//output image to the page
ImageIO. Write (image, "JPEG", the response getOutputStream ());
Out. The clear ();
Out=pageContext. PushBody ();
% & gt;


  • Related