再见!!! 2020-02-07 20:03 采纳率: 0%
浏览 505

JSP实现登录检验功能无法成功实现

登录时,无论输入什么,或者就是空值,点击登录后都是直接输出“用户名或密码错误”想知道如何解决这个问题!!!!!

# **index.jsp**
```<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <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">
        <title>大学生宿舍管理系统</title>
        <style type="text/css">
            body {
                width:1300px;

                background-image: url("imgs/222.jpg");;
                background-repeat: no-repeat;
                background-size:100% 100%;
                background-attachment:fixed;
            }

            #login_frame {
                width: 400px;
                height: 260px;
                padding: 13px;
                position: fixed;
                left: 65%;
                top: 75%;
                margin-bottom: 70%;
                margin-left: -100px;
                margin-top: -200px;
                background-color: rgba(240, 255, 255, 0.25);
                border-radius: 10px;
                text-align: center;
            }

            .text_class {
                width: 180px;
                height: 26px;
                border-top-left-radius: 5px;
                border-top-right-radius: 5px;
                border-bottom-right-radius: 5px;
                border: 0;
            }

            #btn_login {
                font-size: 14px;
                font-family: 宋体;

                width: 100px;
                height: 28px;
                line-height: 28px;
                text-align: center;

                color: white;
                background-color: #3BD9FF;
                border-radius: 6px;
                border: 0;

                float: 160px;
            }
            #btn_regis {
                font-size: 14px;
                font-family: 宋体;
                width: 100px;
                height: 28px;
                line-height: 28px;
                text-align: center;
                color: white;
                background-color: #3BD9FF;
                border-radius: 6px;
                border: 0;

                float: 160px;



            #forget_pwd:hover {
                color: blue;
                text-decoration: underline;
            }

            #login_control {
                padding: 0 28px;

            }
        </style>

     <script type="text/javascript">
             function jianyanIndex()
             {
                 var name = document.frmIndex.username.value;
                 var pwd = document.frmIndex.psaaword.value;
                 if(name=="")
                     {
                       alert("请输入用户名!");
                       return false;
                     }
                 if(pwd=="")
                     {
                       alert("请输入密码!");
                       return false;
                     }
             }

     </script>

    </head>
    <body>
        <div id ="login_frame">
            <form method ="post" action="IndexServer.jsp" name="frmIndex">
                <div>
                    <p><input type="text" id="username" placeholder="请输入用户名" class="text_class"></p>
                </div>

                <div>
                    <p><input type="password" id="password" placeholder="请输入密码" class="text_class"></p>
                </div>

                 <div id="login_control">
                    <input type="submit" id="btn_login" value="登录" onclick="jianyanIndex(;)" />

                    <input type="button" id="btn_regis" value="注册" onclick="window.location.href='register.jsp'"/>


                </div>            
            </form>
         </div> 
    </body>
</html>


IndexServer.jsp****

<%@ page language="java" import="java.sql.*" contentType="text/html;charset=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>登录检验</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>

  <body>
    <br>
   <%
   request.setCharacterEncoding("utf-8");
   String users=request.getParameter("username");
   String pass=request.getParameter("password");
   %>
   <% 


   String driver = "com.mysql.jdbc.Driver";  
   String url = "jdbc:mysql://localhost:3306/success?serverTimezone=GMT";  
   String use = "root";   
   String Password = "fxnbdr15820!";  
   Class.forName(driver);  
   Connection conn= DriverManager.getConnection(url,use,Password); 

   String sql="select * from student where username='"+users+"'and password='"+ pass +"'";
   Statement stmt=conn.createStatement();

   ResultSet rs=stmt.executeQuery(sql);
           if(rs.next())
                 {
                    session.setAttribute("uname","users");
                    out.print("登录成功!!!!");
                  }else
                      {
                        out.print("用户名不存在或密码错误!!!!");
                       }
            rs.close();
            stmt.close();
            conn.close();



    %>



  </body>
</html>



  • 写回答

2条回答 默认 最新

  • DeathAndLife 2020-02-07 21:50
    关注

    String sql="select * from student where username='"+users+"'and password='"+ pass +"'";
    你这句代码问题是
    sql = select * from student where username=usernameand password=12345
    在用户名和and之间没有空格
    你先看看是不是这个问题,如果不是你再检查其他代码,这种情况我在写Demo时也遇到过

    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线