JSP实现验证码
验证码的实现分为jsp页面和java类servlet两部分:
jsp页面:
<!doctype html>
<html>
<head>
<title>loginform.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<h3>带有验证码的登陆页面</h3>
<form method="post" action="login.yan" >
用户名:<input type="text" name="usrename" size="10"><br><br/>
密 码:<input type="password" name="password" size="10"><br><br>
验证码:<input type="text" name="usernum" size="4"><img src="loginform"/><br/><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
java类:
package com.csdn.session;
import java.awt.color;
import java.awt.font;
import java.awt.graphics;
import java.awt.image.bufferedimage;
import java.io.bytearrayoutputstream;
import java.io.ioexception;
import javax.imageio.imageio;
import javax.servlet.servletexception;
import javax.servlet.servletoutputstream;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpsession;
public class loginform extends httpservlet {
private static final long serialversionuid = 1l;
public void doget(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
dopost(request, response);
}
private static int width = 80;
private static int height = 20;
public void dopost(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
response.setcontenttype("image/jpeg");
servletoutputstream out=response.getoutputstream();
//printwriter out = response.getwriter();
response.setheader("pragma", "no-cache");
response.setheader("cache-controll", "no-cache");
response.setintheader("expires",0);
//背景
bufferedimage bi = new bufferedimage(width, height,bufferedimage.type_int_rgb);
graphics g = bi.getgraphics();
drawbackground(g);
//随机产生验证码
char[] rands=generatecheckcode();
//system.out.println(fun(rands));
httpsession session=request.getsession();
session.setattribute("num",fun(rands)+"");//验证码==12-9=3;写入sesion对象
drawrands(g,rands);
bytearrayoutputstream bos=new bytearrayoutputstream();
imageio.write(bi, "jpeg", bos);
byte[] buf=bos.tobytearray();//将bi对象里面内容转化成字符
out.write(buf);
response.setcontentlength(buf.length);
out.flush();
out.close();
}
private int fun(char[] rands){
int num1=integer.parseint(rands[0]+"")*10+integer.parseint(rands[1]+"");
int num2=integer.parseint(rands[2]+"");
int num=0;
if(rands[2]=='+'){
num=num1+num2;
}else{
num=num1-num2;
}
return num;
}
private void drawrands(graphics g, char[] rands) {
g.setcolor(color.black);
g.setfont(new font(null,font.italic|font.bold,18));
g.drawstring(" "+rands[0],1,20);
g.drawstring(" "+rands[1],14,17);
g.drawstring(" "+rands[3],30,16);
g.drawstring(" "+rands[2],42,18);
g.drawstring(" "+rands[4],56,16);
}
private char[] generatecheckcode() {
string chars="0123456789";
string char1="+-";
char[] rands=new char[5];
rands[0]=chars.charat((int) (math.random()*2)+1);
rands[1]=chars.charat((int) (math.random()*10));
rands[2]=chars.charat((int) (math.random()*9)+1);
rands[3]=char1.charat((int) (math.random()*2));
rands[4]='=';
return rands;
}
private void drawbackground(graphics g) {
g.setcolor(new color(0xdcdcdc));
g.fillrect(0,0,width,height);
for(int i=0;i<20;i++){
int x=(int) (math.random()*width);
int y=(int) (math.random()*height);
int red=(int) (math.random()*255);
int greed=(int) (math.random()*255);
int blue=(int) (math.random()*255);
g.setcolor(new color(red,greed,blue));
g.drawoval(x, y, 1, 0);
}
}
}
摘自 宋利兴的专栏