Paul Samsotha
Paul Samsotha

Reputation: 209132

Why are all my bean properties null when when running jsp file

I'm trying to make a database login. I have a bean with properties url, username, and password; an html page with the login parameters (a combobox with the url, and input text fields for username and password); and a jsp to process the request. When I run the jsp, all the properties from the bean remain null. I've ran it through the debugger and I can see they're all null.

Here's the relevant HTML:

    <form method = "post" action = "DBLoginInitialization.jsp">
        JDBC URL
        <select name = "url" size = "1">
            <option>jdbc:mysql://localhost/javabook</option>
        </select><br /><br />
            Username <input name = "username" /><br /><br />
            Password <input name = "password" /><br /><br />
        <input type = "submit" name = "Submit" value = "Login" />
        <input type = "reset" value = "Reset" />
    </form>

Here's part of my bean:

    public class DBBean implements java.io.Serializable{
        private Connection connection = null;
        private String username;
        private String password;
        private String url;
        private String driver = "com.mysql.jdbc.Driver";

       /** Initialize Database */
       public void initializeDB() {
           try {
                Class.forName(driver);

                connection = DriverManager.getConnection(url, username, password);
           }
           catch (Exception ex) {
                ex.printStackTrace();
           }
       }
  }   

And JSP:

    <%@page import = "database.DBBean" %>
    <jsp:useBean id = "dbBeanId" scope = "session" 
                      class = "database.DBBean" ></jsp:useBean>
    <jsp:setProperty name = "dbBeanId" property = "*" />

    <body>

        <%-- Problem occurs here when trying to initialize database--%>
        <% dbBeanId.initializeDB(); %>

        <% if (dbBeanId.getConnection() == null) { %>
            Error: Login Failed. Try Again
        <% } 
           else { %>
                <jsp:forward page = "Table.jsp" />
        <% } %>

    </body>

I can't figure out why all my properties are null when trying to intializeDB(). EDIT: all the getter and setter methods are in place.

Upvotes: 0

Views: 546

Answers (1)

isnot2bad
isnot2bad

Reputation: 24464

You do not have properties, but only private fields. Create getters and setters to make bean properties out of them!

Upvotes: 1

Related Questions