为什么我的登录界面不显示验证码?
我是基于Windows XP开发的,开发工具是MyEclipse7.0,发布用tomcat5.5。框架是hibernate3.2+struts2.0+spring2.5,数据库用的SQL Server2000和SP4补丁包。
登录界面login.jsp部分源码:
<tr>
<td align="right">校验码</td>
<td><INPUT maxLength=4 size=16 name="code1" tabindex="3"><img src="showCheckCode.jsp"></td>
</tr>
<%@ page
import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"%>
<%@ page import="java.io.OutputStream"%>
<%@ page contentType="text/html;charset=gb2312" %>
<%!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);
}%>
<%
try {
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);
OutputStream os = response.getOutputStream();
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(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);
}
String sRand = "";
for (int j = 0; j < 4; j++) {
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 * j + 6, 16);
}
session.setAttribute("rand", sRand);
g.dispose();
ImageIO.write(image, "JPEG", os);
os.flush();
os.close();
os = null;
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
} catch (IllegalStateException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
%>
throws IOException使用支持给定格式的任意 ImageWriter 将一个图像写入 File。
[其他解释]
把这句加上.
response.setDateHeader("Content-Type", "image/jpeg");
[其他解释]
<%@ page contentType="text/html;charset=gb2312" %>
改成
<%@ page contentType="image/jpeg" %>
[其他解释]
<img src="../showCheckCode.jsp">
或者
<img src="本级名/showCheckCode.jsp">
都试试
[其他解释]
你点击验证码那张图片的右键,看看那张图片的路径在什么地方,一般是路径的问题,
[其他解释]
把
response.setContentType("image/jpeg"); //必须设置ContentType为image/jpeg
去掉,在<%@page contentType="image/jpeg"定义就可以了,否则好像会报错。
其次
<%@ %>
<%! %>
<% %>
之间的换行符也去掉
<%@ %><%
....
%>
[其他解释]
你在你的项目下所有JSP都不正常,估计不是代码问题
试试重建个项目能正常显示不?
[其他解释]
<%@page contentType="image/jpeg" pageEncoding="UTF-8" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"%>
<%!
//生成随机颜色
Color getRandColor(Random random, int fc, int bc) {
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.setContentType("image/jpeg"); //必须设置ContentType为image/jpeg
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);
//设置图片的长宽 验证码长度
int width=60, height=20, len=4;
String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
int length = base.length();
//创建内存图像
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//获取图形上下文
Graphics g = image.getGraphics();
//创建随机类的实例
Random random = new Random();
//设定图像背景色(因为是做背景,所以偏淡)
g.setColor(getRandColor(random,200,250));
g.fillRect(0, 0, width, height);
//备选字体
String[] fontTypes = {"tahoma","Atlantic Inline","fantasy","Times New Roman","Georgia","Arial", "Helvetica", "sans-serif","System"};
int fontTypesLength = fontTypes.length;
//在图片背景上增加噪点
g.setColor(getRandColor(random,160,200));
g.setFont(new Font("Times New Roman",Font.PLAIN,12));
for (int i=0;i<6;i++)
{
g.drawString("!@#$%^,.;'[javawind.net]/<&*()>:5277",0,5*(i+2));
}
String sRand="",pStr="";
for (int i=0;i<len;i++)
{
int start = random.nextInt(length);
String rand=base.substring(start,start+1);
sRand+=rand;
//设置字体的颜色
g.setColor(getRandColor(random,10,150));
//设置字体
g.setFont(new Font(fontTypes[random.nextInt(fontTypesLength)],Font.BOLD,16));
//将随机验证码画到图片上
pStr = sRand.substring(i,i+1);
if(i==0){
g.drawString(pStr,2,14);
}
if(i==1){
g.drawString(pStr,15,16);
}
if(i==2){
g.drawString(pStr,30,15);
}
if(i==3){
g.drawString(pStr,45,13);
}
}
//将认证码存入session
request.getSession().setAttribute("rand",sRand);
g.dispose();
//输出图象到页面
ImageIO.write(image,"JPEG",response.getOutputStream());
%>
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!
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=80, height=25;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
// g相当于笔
Graphics g = image.getGraphics();
//生成随机类
Random random = new Random();
// 设定背景色
g.setColor(getRandColor(200,250));
// 画一个实心的长方,作为北京
g.fillRect(0, 0, width, height);
//设定字体
g.setFont(new Font("宋体",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="";
// 如果要使用中文,必须定义字库,可以使用数组进行定义
// 这里直接写中文会出乱码,必须将中文转换为unicode编码
String[] str = {"1","2","3","4","5","6","7","8","9"} ;
for (int i=0;i<4;i++){
String rand=str[random.nextInt(str.length)];
sRand+=rand;
// 将认证码显示到图象中
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(rand,16*i+6,19);
}
// 将认证码存入SESSION
session.setAttribute("rand",sRand);
// 图象生效
g.dispose();
// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
out.clear();
out = pageContext.pushBody();
//out.close();
%>