Java generated picture Verification code sample program _java

Source: Internet
Author: User
Tags stringbuffer

Copy Code code as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title> Landing Page </title>
<script type= "Text/javascript" >
function Refresh () {
LOGINFORM.IMAGE.SRC = "creatimage.jsp";
}
</script>
<body>
<form action= "" method = "Post" name= "LoginForm" >
<label> account: <input name= "username" type= "text"/></label><br>
<label> Password: <input name= "password" type= "password"/></label><br>
<label> Verification Code: <input name= "code" type= "text"/></label>
<!--the validation code as a picture processing-->

<input type= "Submit" value= "Landing"/>
</form>
</body>

Copy Code code as follows:

<% @page import= "Java.util.Random"%>
<% @page import= "Java.awt.Graphics"%>
<% @page import= "javax.imageio.*"%>
<% @page import= "java.awt.*"%>
<% @page import= "Java.awt.image.BufferedImage"%>
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<%
Final char[] str = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ',
' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ',
' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ',
' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z '};
int width=100,height=60;
BufferedImage bi = new BufferedImage (Width,height,
BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics g = bi.getgraphics ();
G.setcolor (New Color (200,200,200));
G.fillrect (0, 0, width, height);
Random rnd = new Random ();
StringBuffer sb = new StringBuffer ("");
Produces a four-digit alphanumeric verification code, and the color of each number is immediately
for (int i=0; i<4; i++) {
int num = Rnd.nextint (str.length);
Color c = new Color (rnd.nextint (256),
Rnd.nextint (256), Rnd.nextint (256));
G.setcolor (c);
G.setfont (New Font ("", Font.bold+font.italic, 20));
g.DrawString (str[num]+ "", 10, 17);
Sb.append (Str[num]);
}
Jamming line
for (int i=0; i<10; i++) {
Color c = new Color (rnd.nextint (256),
Rnd.nextint (256), Rnd.nextint (256));
G.setcolor (c);
G.drawline (Rnd.nextint (width), rnd.nextint (height),
Rnd.nextint (width), rnd.nextint (height));
}
string s = new string (SB);
/*
If four digits are generated, then Nextint (8999) + 1000;
String.valueof then converts to string
*/
Verification code stored in the session, to facilitate the landing in the school check page comparison
Session.setattribute ("image", s);
Output to Page
Imageio.write (BI, "JPEG", Response.getoutputstream ());
/*
What's the effect of adding the following two sentences?
Otherwise reported abnormal: Java.lang.IllegalStateException:getOutputStream ()
has already been called for this response
Whatever the reason.
*/
Out.clear ();
out = Pagecontext.pushbody ();

%>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title> Picture Generation </title>
<body>
</body>

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.