2个回答
展开全部
A 生成验证码的java文件
package cn.com.data100.web.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Title: 生成验证码
* Description: data100 website
* Copyright: Copyright (c) 2011
* Company: km100
* @author anshufa
* @version 1.0
*/
public class RandomCodeServlet extends HttpServlet {
private static final Log log = LogFactory.getLog(RandomCodeServlet.class);
private Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
//设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
Random random = new Random();
// 定义数组存放加减乘除四个运算符
// char[] arr = { '+', '-','*', '/' };
char[] arr = { '+', '-','*'};
// 生成10以内的随机整数num1
int num1 = random.nextInt(10);
// 生成一个0-4之间的随机整数operate
int operate = random.nextInt(3);
// 生成10以内的随机整数num1
int num2 = random.nextInt(10);
//避免出现负数情况
if(operate == 1){//减法运算
//如果减数小于被减数 再次生成num1
while(num1<num2){
num1 = random.nextInt(10);
}
}
// // 避免出现除数为0的情况
// if (operate == 3) {
// // 如果是除法,那除数必须不能为0,如果为0,再次生成num2
// while (num2 == 0) {
// num2 = random.nextInt(10);
// }
// }
// 运算结果
int result = 0;
// 假定position值0/1/2/3分别代表"+","-","*","/",计算前面操作数的运算结果
switch (operate) {
case 0:
result = num1 + num2;
break;
case 1:
result = num1 - num2;
break;
case 2:
result = num1 * num2;
break;
// case 3:
// result = num1 / num2;
// break;
}
log.info("验证码:"+num1 + "," + num2 + "," + operate + "," + result);
// 将生成的验证码值(即运算结果的值)放到session中,以便于后台做验证。
HttpSession session = request.getSession();
session.setAttribute("rand", String.valueOf(result));
int width = 60, height = 20;
//创建BufferedImage对象,设置图片的长度宽度和色彩。
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
OutputStream os = response.getOutputStream();
//取得Graphics对象,用来绘制图片
Graphics g = image.getGraphics();
//绘制图片背景和文字,释放Graphics对象所占用的资源。
g.setColor(getRandColor(199, 250));
//设置内容生成的位置
g.fillRect(0, 0, width, height);
//设置内容的字体和大小
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
//设置内容的颜色:主要为生成图片背景的线条
g.setColor(getRandColor(160, 200));
//图片背景上随机生成155条线条,避免通过图片识别破解验证码
for (int i = 0; i < 355; 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);
}
//构造运算表达式
String content = num1+" "+ arr[operate]+" "+num2+" = ";
//设置写运算表达的颜色
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
//在指定位置绘制指定内容(即运算表达式)
g.drawString(content,5,16);
//释放此图形的上下文以及它使用的所有系统资源,类似于关闭流
g.dispose();
//通过ImageIO对象的write静态方法将图片输出。
ImageIO.write(image, "JPEG", os);
os.close();
}
}
B 页面调用
<span class="tabboder" id="rands"><img
src="../RandomCodeServlet" id="auth" name="auth" border=0></span>
package cn.com.data100.web.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Title: 生成验证码
* Description: data100 website
* Copyright: Copyright (c) 2011
* Company: km100
* @author anshufa
* @version 1.0
*/
public class RandomCodeServlet extends HttpServlet {
private static final Log log = LogFactory.getLog(RandomCodeServlet.class);
private Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
//设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
Random random = new Random();
// 定义数组存放加减乘除四个运算符
// char[] arr = { '+', '-','*', '/' };
char[] arr = { '+', '-','*'};
// 生成10以内的随机整数num1
int num1 = random.nextInt(10);
// 生成一个0-4之间的随机整数operate
int operate = random.nextInt(3);
// 生成10以内的随机整数num1
int num2 = random.nextInt(10);
//避免出现负数情况
if(operate == 1){//减法运算
//如果减数小于被减数 再次生成num1
while(num1<num2){
num1 = random.nextInt(10);
}
}
// // 避免出现除数为0的情况
// if (operate == 3) {
// // 如果是除法,那除数必须不能为0,如果为0,再次生成num2
// while (num2 == 0) {
// num2 = random.nextInt(10);
// }
// }
// 运算结果
int result = 0;
// 假定position值0/1/2/3分别代表"+","-","*","/",计算前面操作数的运算结果
switch (operate) {
case 0:
result = num1 + num2;
break;
case 1:
result = num1 - num2;
break;
case 2:
result = num1 * num2;
break;
// case 3:
// result = num1 / num2;
// break;
}
log.info("验证码:"+num1 + "," + num2 + "," + operate + "," + result);
// 将生成的验证码值(即运算结果的值)放到session中,以便于后台做验证。
HttpSession session = request.getSession();
session.setAttribute("rand", String.valueOf(result));
int width = 60, height = 20;
//创建BufferedImage对象,设置图片的长度宽度和色彩。
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
OutputStream os = response.getOutputStream();
//取得Graphics对象,用来绘制图片
Graphics g = image.getGraphics();
//绘制图片背景和文字,释放Graphics对象所占用的资源。
g.setColor(getRandColor(199, 250));
//设置内容生成的位置
g.fillRect(0, 0, width, height);
//设置内容的字体和大小
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
//设置内容的颜色:主要为生成图片背景的线条
g.setColor(getRandColor(160, 200));
//图片背景上随机生成155条线条,避免通过图片识别破解验证码
for (int i = 0; i < 355; 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);
}
//构造运算表达式
String content = num1+" "+ arr[operate]+" "+num2+" = ";
//设置写运算表达的颜色
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
//在指定位置绘制指定内容(即运算表达式)
g.drawString(content,5,16);
//释放此图形的上下文以及它使用的所有系统资源,类似于关闭流
g.dispose();
//通过ImageIO对象的write静态方法将图片输出。
ImageIO.write(image, "JPEG", os);
os.close();
}
}
B 页面调用
<span class="tabboder" id="rands"><img
src="../RandomCodeServlet" id="auth" name="auth" border=0></span>
2013-08-05
展开全部
找一个生成验证码的Java文件,然后在JSP页面中调用这个对象(一般情况下将相应的变量存储在session中),然后在提交后的页面中处理即可。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |