为什么我的方法没有收到我的布尔值?

问题描述 投票:0回答:1

我的servlet中有一个登录位。

else if (userPath.equals("/Login")) {
        String LuName = request.getParameter("LuserName");
        String Lpass = request.getParameter("Lpassword");
        boolean log = Boolean.parseBoolean(request.getParameter("loggedIn"));
        JavaSqlDb objDb2 = new JavaSqlDb("LineEquation");
        objDb2.SelectDataReturn(LuName, Lpass, log);
            String myUrl = "Welcome" + ".jsp";
            try {
                // Redirect back to the new URL with new data. Sending request from servlet to JSP.
                if (log == true) {
                request.getRequestDispatcher(myUrl).forward(request, response);
                }
                else {
                    System.out.println("Ya goofed.");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }

    }

寻找其获取的值的方法。

public boolean SelectDataReturn(String LuserName, String Lpassword, boolean loggedIn) {
    // Public boolean or void?
    Statement s = null;
    ResultSet rs = null;
    String dbQuery = "SELECT * FROM Users WHERE userName = '" + LuserName + "' AND  password = '" + Lpassword + "' ";
    try {
        s = this.dbConn.createStatement();
        rs = s.executeQuery(dbQuery);
        if (rs.next()) {
            LuserName = rs.getString(1);
            Lpassword = rs.getString(2);
            //loggedIn = rs.getBoolean(3);
            loggedIn = true;
            System.out.println(dbQuery + "found.");
        } else {
            //LuserName = rs.getString(1);
            //Lpassword = rs.getString(2);
            loggedIn = false;
            System.out.println(dbQuery + "not found.");
        }
        String connectionURL = "jdbc:mysql://localhost:3306/" + this.dbName;
        this.dbConn = DriverManager.getConnection(connectionURL, "root", "mysql1");
        Statement st = this.dbConn.createStatement();
        st.executeQuery(dbQuery);
        dbConn.close();
    } catch (SQLException se) {
        System.out.println("SQL Error: Not able to find data.");
        se.printStackTrace(System.err);
        System.exit(0);
    }

    return loggedIn;
}

和主要方法。

public static void main(String[] args) {
    String tableName = "Users";
    ArrayList<ArrayList<String>> myData;
    String dbName = "LineEquation";
    String[] tableHeader = {"userName", "password"};
    JavaSqlDb objDb2 = new JavaSqlDb(dbName);
    myData = objDb2.getData(tableName, tableHeader);
    for (int row = 0; row < myData.size(); row++) {
        System.out.println(myData.get(row));
    }
    objDb2.insertData("b", "2");
    boolean test = objDb2.SelectDataReturn("Dhruv", "pass", false);
}

我是servlet和数据库的相对入门者,因此您可以了解为什么我不明白为什么出于某种原因在文本字段中输入值并提交它们时出现404错误和/或黑屏。我的servlet是否没有接收到布尔值或某种布尔值,我要转到一个不存在的页面吗?

java database authentication servlets
1个回答
0
投票

布尔值是不可变的。如果要以代码示例中的方式使用布尔值,则需要使用可变布尔值。

这篇文章中有一些选择。 Mutable boolean field in Java

© www.soinside.com 2019 - 2024. All rights reserved.