| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 520 人关注过本帖
标题:javaweb的session.isNew ( )有疑惑,附代码
只看楼主 加入收藏
csy294745333
Rank: 1
等 级:新手上路
帖 子:58
专家分:0
注 册:2012-3-27
结帖率:9.09%
收藏
 问题点数:0 回复次数:0 
javaweb的session.isNew ( )有疑惑,附代码
程序代码:
<%@ page contentType="text/html;charset=GB2312"%>
<%!//处理字符串的方法:
    public String codeString ( String s )
    {
        String str = s;
        try
        {
            byte b[] = str.getBytes ( "ISO-8859-1" );
            str = new String ( b );
            return str;
        } catch ( Exception e )
        {
            return str;
        }
    }%>
<HTML>
<BODY>
    <Font size=1> <%@ include file="head.jsp"%>
        <Font size=1> <%
     String string = response.encodeURL ( "userLogin.jsp" );

 %>
            <P>输入用户名和密码:
            <FORM action="<%=string%>" Method="post">
                <BR>登录名称<Input type=text name="logname"> <BR>输入密码<Input
                    type=password name="password"> <BR> <Input type=submit
                    name="g" value="提交">
            </Form> <jsp:useBean id="login" class="a1.Login" scope="session">
            </jsp:useBean> <%
     //提交信息后,验证信息是否正确:
     String message = "" , logname = "" , password = "";
     if ( ! ( session.isNew ( ) ) )///////////这里有疑惑,为什么要加session.isNew ( )?
     {
         logname = request.getParameter ( "logname" );
         if ( logname == null )
         {
             logname = "";
         }
         logname = codeString ( logname );
         password = request.getParameter ( "password" );
         if ( password == null )
         {
             password = "";
         }
         password = codeString ( password );
     }

 %> <%
     if ( ! ( logname.equals ( "" ) ) )
     {

 %> 

 <jsp:setProperty name="login" property="logname" value="<%=logname%>" />

 <jsp:setProperty name="login" property="password" value="<%=password%>" /> <%
     message = login.getMessage ( );
         if ( message == null )
         {
             message = "";
         }
     }

 %> <%
     if ( ! ( session.isNew ( ) ) )
     {
         if ( message.equals ( "ok" ) )
         {
             String str = response.encodeURL ( "buybook.jsp" );
             response.sendRedirect ( str );
         } else
         {
             out.print ( message );
         }
     }

 %>
</Body>
</HTML>

下面是Login.java
程序代码:
package a1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Login
{
    String logname , password , realname , phone , address;
    String success = "false" , message = "";
    Connection con;
    Statement sql;
    ResultSet rs;

    public Login ( )
    { // 加载桥接器:
        try
        {
            Class.forName ( "sun.jdbc.odbc.JdbcOdbcDriver" );
        } catch ( ClassNotFoundException e )
        {
        }
    }

    // 设置属性值、获取属性值的方法:
    public void setLogname ( String name )
    {
        logname = name;
    }

    public String getLogname ( )
    {
        return logname;
    }

    public void setPassword ( String pw )
    {
        password = pw;
    }

    public String getPassword ( )
    {
        return password;
    }

    public void setRealname ( String name )
    {
        realname = name;
    }

    public String getRealname ( )
    {
        return realname;
    }

    public void setPhone ( String ph )
    {
        phone = ph;
    }

    public String getPhone ( )
    {
        return phone;
    }

    public void setAddress ( String ad )
    {
        address = ad;
    }

    public String getAddress ( )
    {
        return address;
    }

    public String getSuccess ( )
    {
        return success;
    }

    // 查询数据库的user表:
    public String getMessage ( )
    {
        try
        {
            con = DriverManager.getConnection ( "jdbc:odbc:shop" , "" , "" );
            sql = con.createStatement ( );
            String condition = "SELECT * FROM user WHERE logname = " + "'" + logname + "'";
            rs = sql.executeQuery ( condition );
            int rowcount = 0;
            String ps = null;
            while ( rs.next ( ) )
            {
                rowcount ++ ;
                logname = rs.getString ( "logname" );
                realname = rs.getString ( "realname" );
                ps = rs.getString ( "password" );
                phone = rs.getString ( "phone" );
                address = rs.getString ( "address" );
            }
            if ( ( rowcount == 1 ) && ( password.equals ( ps ) ) )
            {
                message = "ok";
                success = "ok";
            } else
            {
                message = "输入的用户名或密码不正确";
                success = "false";
            }
            con.close ( );
            return message;
        } catch ( SQLException e )
        {
            message = "输入的用户名或密码不正确";
            success = "false";
            return message;
        }
    }
}

这是书上的例题,代码中已经标明疑惑处,session.isNew()
session不是打开留言其对话才新建的么,那么如果是这样的话应该在第一次打开登陆界面的时候session.isNew()是成立的,也就是不应该进入if语句,不能给相应的String的帐号、密码字段赋值,但是实测代码是没问题的,确实能进入到if ( ! ( session.isNew ( ) ) )这个判断语句里面,请问为什么?我哪里理解错了?他原本if ( ! ( session.isNew ( ) ) )是为了判断什么?
搜索更多相关主题的帖子: javaweb color 
2013-12-28 10:00
快速回复:javaweb的session.isNew ( )有疑惑,附代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.050306 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved