2个回答
展开全部
<%@ 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"/ >
----------------
其他自由发回哈
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"/ >
----------------
其他自由发回哈
展开全部
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判断是否正确
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判断是否正确
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询