读书人

关于jsp的一个有关问题 求大家帮忙~

发布时间: 2012-10-23 12:12:22 作者: rapoo

关于jsp的一个问题 求大家帮忙~~
这是一个关于验证码的小例子
下面是zhuce.jsp

<%@ page language="java" import="java.util.*,javax.servlet.http.*;" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'zhuce.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body style="backgrond:1.jpg no-repeat" >

<form method="get" action="zhuceServlet">

用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/> <br/>

你喜欢什么休闲方式:<br/>

<input type="checkbox" name="aihao" value="读书"/> 读书
 <input type="checkbox" name="aihao" value="旅游">旅游<br/>
<input type="checkbox" name="aihao" value="足球 "/>足球
<input type="checkbox" name="aihao" value="打游戏"/>打游戏<br/>
自我介绍:<br/>
<textarea rows="5" cols="20" name="jianjie"></textarea>
<br>

<img src="image.jsp" width="60" height="20"/><br/>
请输入上面图片
<%
String str=(String)session.getAttribute("rgb");
if(str.equals("0")){
out.print("红");
}
if(str.equals("1")){
out.print("绿");
}
if(str.equals("2")){
out.print("蓝");
}
%>
色的字:

<input type="text" name="rand"/>
<p>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</body>
</html>

下面是image.jsp


<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" pageEncoding="utf-8"%>
<%@ page import="java.io.OutputStream" %>

// 获取随机的rgb
<%!Color getRandColor(int y,int h){
Random random=new Random();
if(h>255)h=255;
if(y>255)y=255;
int r=y+random.nextInt(h-y);
int g=y+random.nextInt(h-y);
int b=y+random.nextInt(h-y);
return new Color(r,g,b);
}
%>
<%
response.setHeader("pragma","No-cache");
response.setHeader("cache","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();
OutputStream os=response.getOutputStream();

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));
//画155条线 干扰 相差32
for(int i=0;i<150;i++){
int x=random.nextInt(width);
int y=random.nextInt(height);
int x1=random.nextInt(12);
int y1=random.nextInt(12);
g.drawLine(x,y,x1,y1);
}

char []c=new char[62];
//获取26个小写字母
for(int i=97,j=0;i<123;i++,j++){
c[j]=(char)i;
}
//获取26个大写字母
for(int o=65,p=26;o<91;o++,p++){
c[p]=(char)o;
}
//获得字符串
String rand="";
int rgb=random.nextInt(3);
int strint=random.nextInt(4);
//颜色数组
Color []color=new Color[3];
color[0]=new Color(255,0,0);
color[1]=new Color(0,255,0);
color[2]=new Color(0,0,255);

for(int i=0;i<4;i++){

if(i==0|i==2){
rand=String.valueOf(random.nextInt(10));
}

if(i==1|i==3){
int x=random.nextInt(52);
rand=String.valueOf(c[x]);
}

if(strint==i){
if(rgb==0){
g.setColor(color[rgb]);
g.drawString(rand,13*i+6,16);
session.setAttribute("rgb",String.valueOf(rgb));
System.out.print("-----"+rgb);
}

if(rgb==1){
g.setColor(color[rgb]);
g.drawString(rand,13*i+6,16);

session.setAttribute("rgb",String.valueOf(rgb));
System.out.print("-----"+rgb);
}
if(rgb==2){
g.setColor(color[rgb]);
g.drawString(rand,13*i+6,16);

session.setAttribute("rgb",String.valueOf(rgb));
System.out.print("-----"+rgb);
}
session.setAttribute("rand",rand);//存储到服务器
continue;
}

g.setColor(new Color(255,255,255));
g.drawString(rand,13*i+6,16);


}

g.dispose();//释放资源

ImageIO.write(image,"JPEG",os);
os.flush();
os.close();
os=null;


response.flushBuffer();
out.clear();
out=pageContext.pushBody();
%>

访问时 会出现空指针异常。。。就是String str=(String)session.getAttribute("rgb");会出现空指针异常。。。测试了很久 不知道什么情况



[解决办法]
情况就是你的session中没有名为"rgb"的对象啦。所以报错。 你可以打印下session.getAttribute("rgb");这个东东试试。

为啥没有呢? 因为你<img src="image.jsp" width="60" height="20"/><br/>
是在image.jsp中设置的哦。 所以按照你的逻辑应该是先执行image.jsp在执行zhuce.jsp 而事实不是的。你直接访问zhuce.jsp那么就解析zhuce.jsp完成后输出内容到浏览器,浏览器在根据img标签请求image.jsp,这个时候image.jsp才解析的。

你可以先访问下image.jsp再访问zhuce.jsp就应该没有错误啦。

如果是我的话我不会这么做。你这里是要一个颜色的选择,你只要从zhuce.jsp中传入一个代表颜色的参数到image.jsp中即可,然后在image.jsp中根据传入的不同颜色参数生成图片。这样还不需要在session中设置东西。本来zhuce.jsp就是image.jsp的触发页面,感觉你放到session中不太好。

你这种注册码的方式可以的。

读书人网 >J2EE开发

热点推荐