session生成验证码图片问题

<%@ page language="java"  contentType="text/html; charset=gb2312" pageEncoding="gb2312"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
   <head>
     <base href="<%=basePath%>">
     
     <title>×××登陆页面×××</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">

   </head>
   <script language="javascript">
   function refImage(){
   document.all.image1.src="/warehouse/image.jsp";
   }
   </script>
   <body>
     <div id="logon_div" class="logon_div_c">
     <form name="logonForm" method="post" action="/warehouse/Logon" >
     <fieldset>
     <legend>请先登陆</legend>    
     <table>
     <c:if test="${!empty sessionScope.user}">
     <tr><td><c:out value="${sessionScope.user}"/>:&nbsp;您已经成功登陆</td></tr>
     <tr><td><a href="/warehouse/validated/index">返回</a></td></tr>
     </c:if>
     <c:if test="${empty sessionScope.user}">
     <tr><td>用户名:</td><td><input id="logon" class="logon_c" type="text" name="logon_name"/></td></tr>
     <tr><td>&nbsp;</td><td><c:if test="${not empty requestScope.logon_name_err}"><font color="red">请输入用户名</font></c:if></td></tr>
     <tr><td>密&nbsp;码:</td><td><input id="psd" class="psd_c" type="password" name="password" /></td></tr>
     <tr><td>&nbsp;</td><td><c:if test="${not empty requestScope.password_err}"><font color="red">请输入密码</font></c:if>&nbsp;</td></tr>
     <tr><td>附加码:</td><td><input id="code_id" class="code_c" type="text" name="code" /></td></tr>
     <tr><td><c:if test="${not empty requestScope.code_err}"><font color="red">请输入附加码</font></c:if>&nbsp;</td><td><img id="image1" src="/warehouse/image.jsp">
     <input type="button" value="刷新图片" οnclick="refImage();"/></td></tr>
     <tr><td>&nbsp;</td><td><input type="submit" value="登 陆" />&nbsp;&nbsp;&nbsp;<input type="reset" value="清 除" /></td></tr>
     </c:if>
     </table>
     </fieldset>
     </form>
      <div>
      <%System.out.println(request.getSession().getId()+" is sessionID"); %>
   </body>
</html>



*******image.jsp******
<%@ page language="java"
import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"
contentType="image/jpeg" pageEncoding="gb2312"%>
<%@ page import="java.lang.Exception,javax.servlet.ServletOutputStream" %>


<%!
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);
System.out.println("session id in image.jsp"+request.getSession().getId());
//在内存中创建图像
int width=60,height=20;
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

//获取图形上下文
Graphics g=image.getGraphics();

//设置背景色
g.setColor(getRandColor(100,250));
g.fillRect(0,0,width,height);

//设定字体和画笔颜色
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
g.setColor(new Color(255,255,255));

//获取随机验证码4位
Random random=new Random();
String validate_code="";
for (int i=0;i<4;i++){
validate_code+=String.valueOf(random.nextInt(10));

}

//画出验证码
g.drawString(validate_code,12,16);

//将验证码加入session
session.setAttribute("code",validate_code);

//图像生效,并释放资源
g.dispose();

//输出验证码图片
try{
ServletOutputStream sos=response.getOutputStream();
ImageIO.write(image,"JPEG",sos);
sos.flush();
sos.close();
sos=null;
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
System.out.println("sessionID in image.jsp is "+session.getId());
}catch (Exception e){
System.err.println("\n error is "+e.toString()+"\n");
}
%>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值