servlet产生随机字母,并用session保存随机字母 在jsp页面用session获得随机字母。但判断老是错误,貌似jsp页面得到是不是加载的图片字母,而是上一个。请问该如何解决??这是产生随机字母的servlet:package com.pet.user.action;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.awt.Color;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.Font;public class ImageServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private static final long serialVersionUID = 8109503217290526554L;
private static final Font FONT = new Font("Times New Roman", Font.ITALIC,
18);
//new Font("Times New Roman",Font.ITALIC,18)
private int defaultWidth; // 图片宽度 private int defaultHeight; // 图片高度 private int defaultLength; // 生成随机字母长度 private String defaultKey = "checkRand"; // 保存在会话中的key值 @Override
public void init() throws ServletException {
super.init();
String key = this.getInitParameter("key");
if (key != null && key.equals("")) {
defaultKey = key;
} String width = this.getInitParameter("width");
try {
defaultWidth = Integer.parseInt(width);
} catch (NumberFormatException e) {
defaultWidth = 60;
} String height = this.getInitParameter("height");
try {
defaultHeight = Integer.parseInt(height);
} catch (NumberFormatException e) {
defaultHeight = 18;
} String length = this.getInitParameter("length");
try {
defaultLength = Integer.parseInt(length);
} catch (NumberFormatException e) {
defaultLength = 4;
}
} private int getImageWidth(String widtha) {
int width = 0;
try {
width = Integer.parseInt(widtha);
} catch (NumberFormatException e) {
width = defaultWidth;
}
return width;
} private int getImageHeight(String heighta) {
int height = 0;
try {
height = Integer.parseInt(heighta);
} catch (NumberFormatException e) {
height = defaultHeight;
}
return height;
} private int getLength(String lengtha) {
int length = 0;
try {
length = Integer.parseInt(lengtha);
} catch (NumberFormatException e) {
length = defaultLength;
}
return length;
} 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);
} //Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setHeader("Pragma", "No-cache"); // 设置响应的文件头
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg"); // 表明生成的响应是图片,而非JSP页面
String key = request.getParameter("key");
if (key == null || key.equals("")) {
key = defaultKey;
}
int width = getImageWidth(request.getParameter("width"));
int height = getImageHeight(request.getParameter("height"));
int length = getLength(request.getParameter("length")); // 在内存中生成图片
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB); // 开始制作图像
Graphics graphics = image.getGraphics();
Random random = new Random();
graphics.setColor(getRandColor(200, 250));
graphics.fillRect(1, 1, width - 1, height - 1);
graphics.setColor(new Color(102, 102, 102));
graphics.drawRect(0, 0, width - 1, height - 1);
graphics.setFont(FONT);
graphics.setColor(getRandColor(160, 200)); // 画随机线条
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int x1 = random.nextInt(6) + 1;
int y1 = random.nextInt(12) + 1;
graphics.drawLine(x, y, x + x1, y + y1);
} // 从另一个方向画随机线条
for (int i = 0; i < 70; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int x1 = random.nextInt(12) + 1;
int y1 = random.nextInt(6) + 1;
graphics.drawLine(x, y, x - x1, y - y1);
} // 生成随机数,并将随机数转换成字母
String sRand = "";
for (int i = 0; i < length; i++) {
int itmp = random.nextInt(26) + 65;
char ctmp = (char) itmp;
sRand += String.valueOf(ctmp);
graphics.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
graphics.drawString(String.valueOf(ctmp), 12 * i + 3, 16);
} //如果布尔值为true且当前没有与请求关联的会话,则使用getSession(boolean value)会创建一个新会话
HttpSession session = request.getSession(true);
session.setAttribute(key,sRand);
graphics.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream()); // 输出图片到Servlet响应
} //Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
} //Clean up resources
public void destroy() {
}
}
这是接收的jsp页面: <tr>
<td>验证码</td>
<td><input type="text" size="6" name="rond"/>
<img src="/JspModule/imageservlet?key=checkRand&width=55"
alt="看不清换一张" width="45" height="18" style="cursor:hand;margin-left:1px;left:10px;"
onclick="reloadImg(this);"/></td>
</tr>判断随机字母:if(document.form1.rond.value!=document.form1.rand.value){
alert("验证码错误!");
document.form1.rond.focus();
return;
}
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.awt.Color;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.Font;public class ImageServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private static final long serialVersionUID = 8109503217290526554L;
private static final Font FONT = new Font("Times New Roman", Font.ITALIC,
18);
//new Font("Times New Roman",Font.ITALIC,18)
private int defaultWidth; // 图片宽度 private int defaultHeight; // 图片高度 private int defaultLength; // 生成随机字母长度 private String defaultKey = "checkRand"; // 保存在会话中的key值 @Override
public void init() throws ServletException {
super.init();
String key = this.getInitParameter("key");
if (key != null && key.equals("")) {
defaultKey = key;
} String width = this.getInitParameter("width");
try {
defaultWidth = Integer.parseInt(width);
} catch (NumberFormatException e) {
defaultWidth = 60;
} String height = this.getInitParameter("height");
try {
defaultHeight = Integer.parseInt(height);
} catch (NumberFormatException e) {
defaultHeight = 18;
} String length = this.getInitParameter("length");
try {
defaultLength = Integer.parseInt(length);
} catch (NumberFormatException e) {
defaultLength = 4;
}
} private int getImageWidth(String widtha) {
int width = 0;
try {
width = Integer.parseInt(widtha);
} catch (NumberFormatException e) {
width = defaultWidth;
}
return width;
} private int getImageHeight(String heighta) {
int height = 0;
try {
height = Integer.parseInt(heighta);
} catch (NumberFormatException e) {
height = defaultHeight;
}
return height;
} private int getLength(String lengtha) {
int length = 0;
try {
length = Integer.parseInt(lengtha);
} catch (NumberFormatException e) {
length = defaultLength;
}
return length;
} 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);
} //Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setHeader("Pragma", "No-cache"); // 设置响应的文件头
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg"); // 表明生成的响应是图片,而非JSP页面
String key = request.getParameter("key");
if (key == null || key.equals("")) {
key = defaultKey;
}
int width = getImageWidth(request.getParameter("width"));
int height = getImageHeight(request.getParameter("height"));
int length = getLength(request.getParameter("length")); // 在内存中生成图片
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB); // 开始制作图像
Graphics graphics = image.getGraphics();
Random random = new Random();
graphics.setColor(getRandColor(200, 250));
graphics.fillRect(1, 1, width - 1, height - 1);
graphics.setColor(new Color(102, 102, 102));
graphics.drawRect(0, 0, width - 1, height - 1);
graphics.setFont(FONT);
graphics.setColor(getRandColor(160, 200)); // 画随机线条
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int x1 = random.nextInt(6) + 1;
int y1 = random.nextInt(12) + 1;
graphics.drawLine(x, y, x + x1, y + y1);
} // 从另一个方向画随机线条
for (int i = 0; i < 70; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int x1 = random.nextInt(12) + 1;
int y1 = random.nextInt(6) + 1;
graphics.drawLine(x, y, x - x1, y - y1);
} // 生成随机数,并将随机数转换成字母
String sRand = "";
for (int i = 0; i < length; i++) {
int itmp = random.nextInt(26) + 65;
char ctmp = (char) itmp;
sRand += String.valueOf(ctmp);
graphics.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
graphics.drawString(String.valueOf(ctmp), 12 * i + 3, 16);
} //如果布尔值为true且当前没有与请求关联的会话,则使用getSession(boolean value)会创建一个新会话
HttpSession session = request.getSession(true);
session.setAttribute(key,sRand);
graphics.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream()); // 输出图片到Servlet响应
} //Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
} //Clean up resources
public void destroy() {
}
}
这是接收的jsp页面: <tr>
<td>验证码</td>
<td><input type="text" size="6" name="rond"/>
<img src="/JspModule/imageservlet?key=checkRand&width=55"
alt="看不清换一张" width="45" height="18" style="cursor:hand;margin-left:1px;left:10px;"
onclick="reloadImg(this);"/></td>
</tr>判断随机字母:if(document.form1.rond.value!=document.form1.rand.value){
alert("验证码错误!");
document.form1.rond.focus();
return;
}
楼主【wujia2009】截止到2008-07-22 18:22:53的历史汇总数据(不包括此帖):
发帖的总数量:2 发帖的总分数:70 每贴平均分数:35
回帖的总数量:1 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:0 结贴的总分数:0
无满意结贴数:0 无满意结贴分:0
未结的帖子数:2 未结的总分数:70
结贴的百分比:0.00 % 结分的百分比:0.00 %
无满意结贴率:---------------------无满意结分率:---------------------
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
<%!
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);
}
%>
<%
//设置页面不缓存
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);// 在内存中创建图象
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// 获取图形上下文
Graphics g = image.getGraphics();//生成随机类
Random random = new Random();// 设定背景色
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);//设定字体
g.setFont(new Font("Times New Roman",Font.PLAIN,18));//画边框
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);
// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160,200));
for (int i=0;i<155;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);
}// 取随机产生的认证码(4位数字)
//String rand = request.getParameter("rand");
//rand = rand.substring(0,rand.indexOf("."));
String sRand="";
for (int i=0;i<4;i++){
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
// 将认证码显示到图象中
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(rand,13*i+6,16);
}// 将认证码存入SESSION
session.setAttribute("ccode",sRand);
// 图象生效
g.dispose();// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
%> 我这儿有个JSP写的.你研究一下.(不是我写的)