用jsp做的登陆页面加一个验证码的功能,怎么实现?求高手指教,验证码要随机生成

如题,谢谢... 如题,谢谢 展开
 我来答
wen5148
2011-10-18
知道答主
回答量:47
采纳率:0%
帮助的人:12.1万
展开全部
<%@ page contentType="image/jpeg"
import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"
pageEncoding="UTF-8"%>
<%
//生成图像
int width = 80, height = 30;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Random random = new Random();

//设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);

//验证码部分
int sRand[] = new int[4];
String s = "";
for (int i = 0; i < 4; i++) {
sRand[i] = (int) (Math.random() * 9);
s += sRand[i];
}

//背静
Graphics g = image.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, 80, 30);

//画数字
for (int i = 0; i < 4; i++) {
g.setColor(Color.BLUE);
g.setFont(new Font("黑体", Font.BOLD, 28));
g.drawString(sRand[i] + "", 20 * i, 20);
}

//画干扰线
for (int i = 0; i < 5; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(width);
int yl = random.nextInt(height);
g.drawLine(x, y, xl, yl);
}

session.setAttribute("checkcode",s);

ImageIO.write(image, "JPEG", response.getOutputStream());
out.clear();
out = pageContext.pushBody();
%>//随便做了一个,其实就是自己画画而已,存为checkcodeimage.jsp放到WEB-ROOT下

前台:
用户名:***
密码:***
验证码:<input class=wenbenkuang name="checkcode"/><img src="checkcodeimage.jsp"/ >
----------------
其他自由发回哈
媒体营销徐杰
2011-10-11 · 超过31用户采纳过TA的回答
知道答主
回答量:268
采纳率:0%
帮助的人:73.7万
展开全部
public class CodeValidation {

private int a;
private int b;
private int c;
private int first;
private int last;

public CodeValidation() {
Random rd = new Random(System.currentTimeMillis());
this.a = rd.nextInt(15)+15;
this.b = rd.nextInt(10)+5;
this.c = rd.nextInt(5);
this.first = rd.nextInt(3);
this.last = rd.nextInt(3);
}

public int getCodeValue() {
int temp = 0;
switch (first) {
case 0:
temp = this.a + this.b;
break;
case 1:
temp = this.a - this.b;
break;
case 2:
temp = this.a * this.b;
break;
case 3:
temp = this.a / this.b;
break;
default:
break;
}

switch (last) {
case 0:
temp = this.c + temp;
break;
case 1:
temp = temp - this.c;
break;
case 2:
temp = this.c * temp;
break;
case 3:
temp = temp / this.c;
break;
default:
break;
}

return temp;
}

public String getCodeStr() {
String temp = new String("");
switch (first) {
case 0:
temp = "(" + this.a + " + " + this.b + ")";
break;
case 1:
temp = "(" + this.a + " - " + this.b + ")";
break;
case 2:
temp = "(" + this.a + " * " + this.b + ")";
break;
case 3:
temp = "(" + this.a + " / " + this.b + ")";
break;
default:
break;
}

switch (last) {
case 0:
temp = temp + " + " + this.c;
break;
case 1:
temp = temp + " - " + this.c;
break;
case 2:
temp = temp + " * " + this.c;
break;
case 3:
temp = temp + " / " + this.c;
break;
default:
break;
}
return temp + " 答案:" + this.getCodeValue();
}

}

-----------------------------------------------------------------------------
JSP页面取得JAVA返回的随机数,然后通过JS判断是否正确
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式