首页 > 解决方案 > 同时使用 JSP 和 Javascript 的问题

问题描述

当 response.sendRedirect 与 Javascript 一起使用时,JS 代码不起作用。如果登录凭据错误,我的目标是让用户留在同一页面上,而是转到空白页面。如果登录错误,则 JS 代码运行并重定向到空白页面。我希望它保持在同一页面上。

这是 HTML/CSS 代码。

<!DOCTYPE html>
<html>
<head>
    <title>Page0</title>
    <style>

        h3 {

    font-family: Calibri;
    font-size: 35pt;
    color: #929292;
    font-style: normal;
    font-weight: bold;
    text-align: center;

        }

    input[type=text] {
        background-color: #E7E5E5;
        width: 300px;
        height: 30px;
        position: relative;
        display: block;
        margin: auto;
        padding: 12px 15px;
        border: none;
    }

    input[type=password] {
        background-color: #E7E5E5;
        width: 300px;
        height: 30px;
        position: relative;
        display: block;
        margin: auto;
        padding: 12px 15px;
        border: none;
    }

div {

    width: 400px;
    height: 450px;
    background-color: white;
    margin: auto;
    left: 0; right: 0; top: 0; bottom: 0;
    position: fixed;
    border-radius: 10px;
}

button {

    display: block;
    margin: auto;
    padding: 15px 118px;
    font-size: 30px;
    color: white;
    border: none;
    background-color: #07CCF5;
}


    </style>
</head>
<body bgcolor="#07CCF5">
<div class="boxed">

    <h3>Login</h3>
    <form action="Page0-0.jsp" method="POST">
        <input type="text" id="txt1" name="Username" placeholder="Enter Username">
        <br><br>
        <input type="password" id="txt2"  name="Password" placeholder="Enter Password">
        <br><br>
        <button type="submit" value="submit">Sign In</button>
        </form>

</body>
</html>


Here is the jsp code.


<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 

<html>
<head></head>
<body>

    <%

    try{
    String s1=request.getParameter("Username");
    String s2=request.getParameter("Password");
    String s3="",s4="";
    Class.forName("com.mysql.jdbc.Driver").newInstance();  
    java.sql.Connection con=DriverManager.getConnection(  
    "jdbc:mysql://localhost:3306/Login","root","");  
    Statement stmt=con.createStatement();  
    ResultSet rs=stmt.executeQuery("select * from log where U_name='"+s1+"'");  
    while(rs.next()){
    s3=rs.getString(1);
    s4=rs.getString(2); 
    }
    if(s1!=""&&s2!=""){
    if(s1.equals(s3)&&s2.equals(s4))
        response.sendRedirect("Page0-1.jsp");
    else
        response.sendRedirect("Page0.jsp");
    }
    else{  

        %>
       <script type="text/javascript">
       alert("Invalid username or password try again");
       </script>
       <%
      //response.sendRedirect("Page0.jsp");
    }   
    con.close();
    }
    catch(Exception e){
    out.print(e.toString());
    }
    %>  

</body>   
</html>

标签: javascripthtmljsp

解决方案


我不是专家,但您的代码中有很多问题,我想分享我对它们的想法。

当您使用 rs.next() 遍历您的用户名和密码集时,您存储它的字符串(即 s3 和 s4)将被覆盖。如果您希望存储所有条目而不是字符串,请使用 ArrayList,然后检查用户名和密码。不过这个方法不是很好。相反,我建议使用下面的代码来检查数据库中的值。

您的“if 和 else”语句是错误的,您正在检查 s1 和 s2 是否为空,然后在它们确实为空的情况下什么也不做。此外,只有最后 2 个“else”语句中的一个将被执行,而不是两者都执行,以防您希望 JS 警报以错误的用户名和密码弹出。

            String uname=request.getParameter("Username");
            String passwd=request.getParameter("Password");
            PreparedStatement ps;
            ResultSet rs;      

            ps=con.prepareStatement("select * from log where username=? and user_password=?");
            ps.setString(1, uname);
            ps.setString(2, passwd);
            rs=ps.executeQuery();

            if(!rs.next()) 
            {   
                 // Your code if username and password don't match
                 // Use only one: JS redirect code or the JSP redirect code
                  //JS alert code here 
            }
            else 
                {
                 //  Your code if username and password match
                 // Use only one: JS redirect code or the JSP redirect code
                 }

将来尝试在单独的 Java 类方法中保持数据库与数据检索方法的连接,并根据需要使用适当的参数和返回类型,并在需要时在 JSP 页面上调用它们。


推荐阅读