<%
        request.setCharacterEncoding("gb2312");
        response.setCharacterEncoding("gb2312");
        
String submit=request.getParameter("submit");
        

        Connection conn = null;
        Statement stmt = null;
         ResultSet rs = null;
if(submit.equals("登录"))
{
                String username=request.getParameter("userName");
        String password=request.getParameter("userPassword");
                try{
                         Class.forName("org.gjt.mm.mysql.Driver").newInstance();
                         conn= DriverManager.getConnection("jdbc:mysql://localhost/mychatroom","root","123");
                        stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
                        String sql="select * from userinfo where username='"+username+"'and userpasswd='"+password+"'";
                        rs=stmt.executeQuery(sql);
                        if(!rs.next())
                        {
                                out.println("<script language='javascript'>alert('用户名或密码错误');parent.location.href='index.jsp';</script>");
                        }
                        else
                        {
                                //Vector temp=(Vector)application.getAttribute("myuser");
                                if(application.getAttribute("userList")==null)
                                {
                                    application.setAttribute("userList", new Vector());                                }                                Vector v = (Vector)application.getAttribute("userList");                                for(int i = 0; i < v.size(); i++)
                                {
                                    String t = (String)v.elementAt(i);                                    if(username.equalsIgnoreCase(t))
                                    {
                                        out.println("<script language='javascript'>alert('该用户已经登录');parent.location.href='index.jsp';</script>");
                                    }
                                }
                                v.addElement(username);
                                application.setAttribute("userList", v);
                                session.setAttribute("username",username);                               response.sendRedirect("frame.jsp");
                        }
                }
                catch(java.sql.SQLException e)
                {
                    out.println(e.toString());
                }
                finally{
                          if(rs!=null)
                              rs.close();
                          if(stmt!=null)
                              stmt.close();
                          if(conn!=null)
                              conn.close();
                    }
}
       
else if(submit.equals("注册"))
{
response.sendRedirect("register.jsp");
}        else if(submit.equals("发送"))
        {
            if(application.getAttribute("ChatMessage")==null)
            {
                application.setAttribute("ChatMessage", new Vector());            }            Vector v = (Vector)application.getAttribute("ChatMessage");
            String msg = session.getAttribute("username") + "说:" + request.getParameter("message") + "<br>";
            v.addElement(msg);
            response.sendRedirect("send.jsp");
        }
        else if(submit.equals("退出聊天室"))
        {
            Vector v = (Vector)application.getAttribute("userList");
            
            v.removeElement((String)session.getAttribute("userName"));
            //v.removeAllElements();
            application.setAttribute("userList", v);
            response.sendRedirect("send.jsp");
        }
         else if(submit.equals("取消"))
         {
             response.sendRedirect("index.jsp");
         }
         else if(submit.equals("确定"))
         {
                String registerName=request.getParameter("registerName");
        String registerPasswd=request.getParameter("registerPasswd");
                String confirmPasswd = request.getParameter("confirmPasswd");
                if(!registerPasswd.equals(confirmPasswd))
                {
                    out.println("<script language='javascript'>alert('密码不一  致');parent.location.href='index.jsp';</script>");
                }                try{
                         Class.forName("org.gjt.mm.mysql.Driver").newInstance();
                         conn= DriverManager.getConnection("jdbc:mysql://localhost/mychatroom","root","123");
                        stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
                        String sql="select * from userinfo where username='"+registerName+"'";
                        rs=stmt.executeQuery(sql);
                        if(rs.next())
                        {
                                out.println("<script language='javascript'>alert('注册失败,用户名已经被注册过了');parent.location.href='index.jsp';</script>");
                        }
                        else
                        {
                                //Vector temp=(Vector)application.getAttribute("myuser");
                                sql="insert into userinfo values ('"+registerName+"','" + registerPasswd + "')";
                                stmt.execute(sql);
                               out.println("<script language='javascript'>alert('注册成功');parent.location.href='index.jsp';</script>");
                        }
                }
                catch(java.sql.SQLException e)
                {
                    out.println(e.toString());
                }
                finally{
                          if(rs!=null)
                              rs.close();
                          if(stmt!=null)
                              stmt.close();
                          if(conn!=null)
                              conn.close();
                    }
         }
%>在判断用户是否已经登录那里,和退出聊天室那里老是判断不出来,是什么原因,是编码问题还是???要怎么改

解决方案 »

  1.   

    在判断用户是否已经登录那里是if(username.equals(t)),上面的打错了
      

  2.   

     Vector v = (Vector)application.getAttribute("userList");                                for(int i = 0; i < v.size(); i++)
                                    {
                                        String t = (String)v.elementAt(i);                                    if(username.equals(t))
                                        {
                                            out.println("<script language='javascript'>alert('该用户已经登录');parent.location.href='index.jsp';</script>");
                                        }
                                    }
    我跟踪了一下,发现它有进入if(username.equals(t))
    但是没有跳转,是什么原因,谁找出原因我加他100分
      

  3.   


    parent.location.href='index.jsp';
    改为
    parent.location='index.jsp';试试看。
      

  4.   

     out.println(" <script language='javascript'>alert('该用户已经登录'); </script>"); 先看看是否输出了 
      

  5.   

    我来接分你代码里先
    out.println("<script language='javascript'>alert('该用户已经登录');parent.location.href='index.jsp';</script>");然后 
    response.sendRedirect("frame.jsp");只会跳转到frame.jsp。你在out.println()后面加return;应该就行了
    for(int i = 0; i < v.size(); i++)
                                    {
                                        String t = (String)v.elementAt(i);                                    if(username.equalsIgnoreCase(t))
                                        {
                                            out.println("<script language='javascript'>alert('该用户已经登录');parent.location.href='index.jsp';</script>");
    return;
                                        }
                                    }
      

  6.   

    1.根据parent.location.href='index.jsp';看楼主的意思是要父页面跳转,我不知道你这个页面是怎么打开的,不过按照你这种方法是不能实现跳转的(至少我没试验成功),想实现父页面跳转可以使用下面的方法
    父页面:
    <script type="text/javascript">
    function openWindow(url) {
           newWindow = window.open(url, "", "");
    }</script>   <input type="button" onclick="openWindow('test2.jsp');" value="jjjjj">子页面:
     <script type="text/javascript">
      function submit()
      {
       window.opener.location="http://www.baidu.com";
       window.close();
      }
      </script>
    <input type="button" onclick="submit();" value="tiaozhuan">2.修改你的代码
     if(username.equalsIgnoreCase(t))
    {
      out.println("<script language='javascript'>alert('用户名或密码错误');window.opener.location='http://www.baidu.com';</script>");
    }
    将打开页面的连接换做使用button传参数的方式,参考我上面的代码。
      

  7.   

    for(int i = 0; i < v.size(); i++)
                                    {
                                        String t = (String)v.elementAt(i);                                    if(username.equalsIgnoreCase(t))
                                        {
                                            out.println("<script language='javascript'>alert('该用户已经登录');parent.location.href='index.jsp';</script>");
                                        }
                                    }
                                    v.addElement(username);
                                    application.setAttribute("userList", v);
                                    session.setAttribute("username",username);                               response.sendRedirect("frame.jsp");你这里有问题吧,因为jsp是动态语言,先要把所有的<%%>等动态的标签解析成HTML,所以到response.sendRedirect("frame.jsp");的时候就已经跳到frameset.jsp里了你把这句注释掉试下