JSP Verification Code

Source: Internet
Author: User
Tags rand

A. JSP page

<script language= "javascript" type= "Text/javascript" >
Refresh Verification Code
function Showrand () {
document.getElementById ("random"). src = "<%=path%>/common/image.jsp?date=" + new Date ();
}

</script>

<body>

<p class= "test" id= "P_randcode" style= "Display:none" >
<b> Verification Code </b>
<input type= "text" value= "" class= "Txt_change" id= "Confirmcode" name= "Confirmcode" >
<a style= "Cursor:pointer;align:left" onclick= "Showrand ();" > Change one </a>
<input type= "hidden" id= "Confirmrand" name= "Confirmrand"/>
</p>

</body>

Two. Display the image.jsp of the Verification code picture

<%@ page contenttype= "image/jpeg" import= "com.sun.image.codec.jpeg.*,java.awt.image.*,java.awt.*,java.util.*"% >
<%
Request.setcharacterencoding ("GBK");
Response.setcontenttype ("text/html; CHARSET=GBK ");


String rand = "0000";//randomly generated verification code


if (Request.getparameter ("rand") = null) {
Rand = (String) request.getparameter ("Rand");
Request.getsession (). SetAttribute ("Rand", Rand);
}
try{
Out.clear ();
Response.setcontenttype ("Image/jpeg");
Response.AddHeader ("Pragma", "no-cache");
Response.AddHeader ("Cache-control", "No-cache");
Response.adddateheader ("Expries", 0);
int width =, height = 20;
BufferedImage image = new BufferedImage (width, height,
BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics g = image.getgraphics ();
Random random = new random ();
int fc = $, BC = 255;
if (FC > 255) {
FC = 255;
}
if (BC > 255) {
BC = 255;
}
int red = FC + Random.nextint (BC-FC);
int green = FC + Random.nextint (BC-FC);
int blue = FC + Random.nextint (BC-FC);
Color color = new color (red, green, blue);

G.setcolor (color);
G.setfont (New Font ("Times New Roman", Font.plain, 18));
G.fillrect (0, 0, width, height);

for (int i = 0; i <; 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);
}

int stringlength = 4;
String srand= "";
for (int i = 0; i < stringlength; i++) {
String randstr = string.valueof (Rand.charat (i));
SRAND+=RANDSTR;
G.setcolor (New Color (+ random.nextint), + random
. Nextint (+), + random.nextint (110));
g.DrawString (RANDSTR, * i + 6, 16);
}
Session.setattribute ("Ccode", SRand);
G.dispose ();

Servletoutputstream OutStream = Response.getoutputstream ();
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (OutStream);
Encoder.encode (image);
Outstream.close ();
Out.clear ();
out = Pagecontext.pushbody ();
} catch (Exception e) {
}
%>

JSP Verification Code

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.