CAPTCHA Code Writing

Source: Internet
Author: User

index.jsp page

<%@ page contenttype= "text/html; Charset=utf-8 "%>
<%@ taglib uri= "/web-inf/struts-bean.tld" prefix= "Bean"%>
<%@ taglib uri= "/web-inf/struts-html.tld" prefix= "html"%>
<%@ taglib uri= "/web-inf/struts-logic.tld" prefix= "logic"%>
<title><bean:message key= "Application.title"/></title>
<link rel= "icon" href= "./images/favicon.ico" type= "Image/x-icon"/>
<link rel= "stylesheet" href= "/styles/login.css" type= "Text/css"
Media= "All"/>
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>
<meta name= "Author" content= "Neusoft"/>
<meta name= "description" content= "MCIS System"/>
<meta name= "Copyright" content= "Copyright (c) Neusoft"/>
<style type= "Text/css" >
<!--
. style1 {border-color: #009900}
-
</style>
<body onload= "load ();" >
<div id= "Layout-left" ></div>
<div id= "Layout-middle" >

<div id= "Body" >
<div id= "Body_left" >
<p></p>
</div>
<div id= "Body_right" >
Onsubmit= "return Clicksubmit ();" >
<ul>
<li></li>
<li>
User name:
<br>
Onkeyup= "Checkadmin ();" Onblur= "checkadmin ();"
Style= "width:160;height:20" maxlength= "/>"
</li>
<li>
Secret &AMP;NBSP&AMP;NBSP Code:
<br>
<!--<input class= "fixed" type= "password" size= "+" id= "password"/>-->
Style= "width:160;height:20" maxlength= "ten"/>
</li>
<li>
Verification Code:
<br>
Maxlength= "4" style= "width:100;height:20" value= ""
Onfocus= "if (this.value== ') this.value=";
Onblur= "if (this.value== ') {this.value=";} "/>
<a href= "Javascript:show (document.getElementById (' Hcode '))" >Id= "Hcode" title= "click to refresh"
Src= "./random.jsp" hspace= "class=" Style1 "
Style= "Text-decoration:none"/></a>
</li>
<li>
Login method:
Property= "User.value (TYPE)" value= "0"/>
Exclusive mode
</li>
<li>
<input class= "BT" type= "Submit" value= "Login"/>
<!--<a href= "#" > Forgot password?</a>-->
<input class= "BT" type= "button" value= "Reset"
Onclick= "Clickreset ();" >
</li>
</ul>
<div id= "Message" >
<%@ include file= ". /common/messagehandle.jsp "%>
</div>
<span id= "logo" ></span>
</div>
</div>

</div>
<div id= "Layout-right" ></div>

<script type= "Text/javascript" >


<!--
var windowname = "Processwindow";

function init () {
Settitle2top ();
Window.name = Windowname;
}

function OnEnter () {

if (Window.event.keyCode = =)//if press the Enter key
Window.event.keyCode = 9; Replace it with the Tab key
Userbean.submit ();
}
function Checkadmin () {

if (' admin ' = = userbean.elements["User.value (ID)"].value) {
document.getElementById ("Admintype"). Disabled=false;
}else{
document.getElementById ("Admintype"). Disabled= "true";
}
}

function Clicksubmit () {
var main = document.getElementById ("main");
var errmsg = document.getElementById ("message");
var id=userbean.elements["User.value (Id)"].value;
var orignpass=userbean.elements["User.value (PASSWORD)"].value;
var rs=userbean.elements["User.value (RS)"].value;
var orignrs=userbean.elements["User.value (RS)"].value;

if (Id = = NULL | | id== "") {
Errmsg.style.color= "#CC0000";
Errmsg.innertext = "Please enter user name";
userbean.elements["User.value (ID)"].focus ();
return false;
}
if (Orignpass = = NULL | | orignpass== "") {
Errmsg.style.color= "#CC0000";
Errmsg.innertext = "Please enter password";
userbean.elements["User.value (PASSWORD)"].focus ();
return false;
}

if (Orignrs = = NULL | | orignrs== "") {
Errmsg.style.color= "#CC0000";
Errmsg.innertext = "Please enter the verification code";
Userbean.elements["User.value (RS)"].focus ();
return false;
}
else {
return true;
Userbean.submit ();
}
}
function Clickreset () {
Userbean.reset ();
Load ();
var errmsg = document.getElementById ("message");
Errmsg.innertext= "";
}
function Load () {

document.getElementById ("Type1"). Checked= "true";
Alert (document.getElementById ("type2"). Disabled);
document.getElementById ("Admintype"). Disabled=true;
}

function Show (o) {//Reload Verification code
var timenow = new Date (). GetTime ();
O.src= "./random.jsp?d=" +timenow;
Timeout execution;
SetTimeout (function () {
O.src= "./random.jsp?d=" +timenow;
}
);
}
-
</script>
</body>

random.jsp

<%@ page contenttype= "IMAGE/JPEG; CHARSET=GBK "%>
<%@ page import= "java.awt.*,java.awt.image.*"%>
<%@ page import= "java.util.*"%>
<%@ page import= "com.sun.image.codec.jpeg.*"%>


<%!//generates a random color function getrandcolor
Color getrandcolor (int fc, int BC) {
Random r = new Random ();
if (FC > 255) FC = 255;
if (BC > 255) BC = 255;
int red = FC + R.nextint (BC-FC); Red
int green = FC + R.nextint (BC-FC); Green
int blue = FC + R.nextint (BC-FC); Blue
return new Color (red, green, blue);
}%>
<%
Settings page does not cache
Response.setheader ("Pragma", "No-cache");
Response.setheader ("Cache-control", "No-cache");
Response.setdateheader ("Expires", 0);

Create a random class
Random r = new Random ();

Create an image in memory with width widths and height
int width =, height = 20;
BufferedImage pic = new BufferedImage (width, height, bufferedimage.type_int_rgb);

Get the graphical context environment
Graphics GC = Pic.getgraphics ();

Set Background shape context fill
Gc.setcolor (Getrandcolor (200, 250));
Gc.fillrect (0, 0, width, height);

Setting the graphics context font
Gc.setfont (New Font ("Times New Roman", Font.plain, 18));

Randomly generates 50 lines of interference so that the authentication code in the image is not easily detected by other analysis programs
Gc.setcolor (Getrandcolor (160, 200));
for (int i = 0; i < i++) {
int x1 = r.nextint (width);
int y1 = r.nextint (height);
int x2 = r.nextint (15);
int y2 = r.nextint (15);
Gc.drawline (x1, y1, x1 + x2, y1 + y2);
}

Randomly generate 50 interference points, so that the verification code in the image is not easily detected by other analysis programs
Gc.setcolor (Getrandcolor (120, 240));
for (int i = 0; i < i++) {
int x = r.nextint (width);
int y = r.nextint (height);
Gc.drawoval (x, y, 0, 0);
}
Randomly generate 4-digit verification codes
String RS = "";
String rn = "";
for (int i = 0; i < 4; i++) {
Generates a random digital RN within 10
RN = string.valueof (R.nextint (10));
RS + = RN;
Display the authentication code in the image using the DrawString function
Gc.setcolor (New Color (+ r.nextint), + r.nextint (+), + R
. Nextint (110)));
Gc.drawstring (RN, * i + 6, 16);
}

Releasing the graphical context environment
Gc.dispose ();

Share the authentication code RS into session
Session.setattribute ("random", RS);

Output the generated CAPTCHA image to the page
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (Response.getoutputstream ());
Encoder.encode (pic);

Out.clear ();
out = Pagecontext.pushbody ();
%>

CAPTCHA Code Writing

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.