0

I've got a login bit in my 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();
            }

    }

A method to look for the values it's getting.

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;
}

And a main method.

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);
}

I'm a relative beginner in servlets and databases, so you could understand how I don't understand why, for some reason, when I enter values into textfields and submit them, I get a 404 error and/or a blank screen. Is my servlet not receiving my boolean value or something of the sort, am I going to a page that doesn't exist?

  • 1
    You are assigning a new value to a boolean parameter in a method, and expecting that this change will be visible to the caller. This is not the case. Java uses [pass-by-value semantics](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). You should remove the boolean parameter and just return true/false instead. – that other guy Feb 26 '20 at 22:19

1 Answers1

0

boolean values are immutable. If you want to use a boolean in the way you want to in your code example you need to use a mutable boolean.

There are some options in this post. Mutable boolean field in Java

mavisto
  • 31
  • 4