问题是这样的,我的WEB服务器是TOMCAT 5,写了个登陆页面,其中验证码图片是通过<jsp:include page="/image.jsp"/>引入的,服务器在本机上,通过本机浏览器输入验证码登陆没问题,登陆页面logon.jsp的session id 和验证码生成页面image.jsp打印出来的一致。可只要通过其他计算机访问本机登陆页面,输入验证码提交后,老是提示验证码错误,在image.jsp中产生的验证码我已经加入到了session中( request.getSession().setAttribute("code",code); ),在2个页面中打印session id 比较后,发现不一样,太奇怪了,有知道原因的朋友吗?请不吝赐教!

解决方案 »

  1.   

    include的session也是一样的查看一下是不是远程的时候图片被缓存了吧。
      

  2.   

    应该不是缓存问题jsp页面中都加了如下代码
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    所以,应该不是缓存的问题,而且在本机上的浏览器访问一点问题都没有 ,验证码图片生成页面和登陆页面 session.getId()打印出来都一致。且无论访问TOMCAT上任何页面session id 都一样保持不变。
    但只要从其他计算机访问本机,每次访问页面后,session都不一样,这就造成我保存在session中的属性值无法获得。先将登陆页面,验证码生成页面,以及登陆提交后,验证登陆信息的servlet代码贴出来,请大家帮我看下问题出在哪里。*******logon.jsp******
    <%@ 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="刷新图片" onclick="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");
    }
    %>
      

  3.   

    我检查过了,不是jsp:include的问题
    问题是:在通过其他计算机访问本机时,登陆页面与验证码生成页面的session不一致
    哪位知道是为什么?
      

  4.   

    <meta http-equiv="pragma" content="no-cache"> 
    <meta http-equiv="cache-control" content="no-cache"> 
    <meta http-equiv="expires" content="0"> 去掉他们吧!他们会使cookie实效的!具体哪一个我不记得了!
      

  5.   

    感谢 java2000_net的回复,可能不是你说的这个原因,因为在本机上是没问题的,只是从其他计算机上访问本机tomcat页面时才出现2个页面的session 不一致。
    而且,今早又试了下,居然同样的代码,什么都没改变过,无论是本机访问还是其他计算机访问居然又都没问题了,session完全一致。
    唉!!!就怕出现这种情况,让你查都没办法查!
    晕死了都要。
      

  6.   


    java2000_net ,我找到原因了,如果在浏览器里输入http://ip地址:端口号/logon.jsp 是没有问题的(无论是本机访问,还是远程机访问都是正常的),可一旦使用 http://主机名:端口号/logon.jsp那么就会发生session不一致的问题,即使在本机上访问,也是如此。之前因为在本机上使用 http://localhost:端口号/logon.jsp所以未发现此问题,而在其他计算机上却使用了 http://主机名:端口号/logon.jsp 发生了session不一致。但具体为什么会出现这种情况,还是不太明白。