0

I am trying to connect my JSP servlets to a posgress database and I am currently using a java bean class which is playing the role of the middle man. I am experiencing some difficulties with making the registration form successfully store user information into the database. I would really appreciate if you would kindly help me out.

Thanks a lot in advance.

JSP servlet:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Register here</title>
</head>
<body>
    <form method="post" action="registration.jsp">
        <center>
        <table border="1" width="30%" cellpadding="5">
            <thead>
                <tr>
                    <th colspan="2">Enter Information Here</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>First Name</td>
                    <td><input type="text" name="fname" value="" /></td>
                </tr>
                <tr>
                    <td>Last Name</td>
                    <td><input type="text" name="lname" value="" /></td>
                </tr>
                <tr>
                    <td>Email</td>
                    <td><input type="text" name="email" value="" /></td>
                </tr>
                <tr>
                    <td>User Name</td>
                    <td><input type="text" name="uname" value="" /></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td><input type="password" name="pass" value="" /></td>
                </tr>
                <tr>
                    <td>Current Country</td>
                    <td><input type="text" name="country" value="" /></td>
                </tr>
                <tr>
                   <td>Current City</td>
                   <td><input type="text" name="city" value="" /></td>
                </tr>   
                <tr>
                    <td><input type="submit" value="Submit" /></td>
                    <td><input type="reset" value="Reset" /></td>
                </tr>
                <tr>
                    <td colspan="2">Already have an account? <a href="index.jsp">Login Here</a></td>
                </tr>
            </tbody>
        </table>
        </center>
    </form>
</body>

The Java Bean that I use :

public class UserBean {

    private int id;
    private String username;
    private String password;
    private String email;
    private String firstName;
    private String lastName;
    private String endDate;
    private boolean validated;

    public UserBean() {
        // Empty constructor
    }

    public int getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getEmail() {
        return email;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getEndDate() {
        return endDate;
    }

    public boolean isValidated() {
        return validated;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    public void setValidated(boolean validated) {
        this.validated = validated;
    }
}
2
  • I also have a main java class for all the beans that I use and they are all stored as a package under "WEB-INF/classes" Commented Apr 8, 2014 at 0:37
  • Help you with what? You said your problem is with saving data to the database, but you haven't said what the problem is nor shown how you save the data. Commented Apr 8, 2014 at 1:27

1 Answer 1

1

Your POJO JavaBean won't magically get populated with the data. It has no connection to the database and no way to get or save data.

You need a controller that fetches data from the DB, creates model objects, and populates them with the data. The controller is also responsible for saving beans

You could write this yourself but it's generally better to use existing ORM frameworks like JPA2, a custom persistence provider API like Hibernate, or something like MyBatis. If you really want, you can hand-roll your controller with direct JDBC calls, injecting the connection from the environment, but that tends to produce a lot of boilerplate code for little benefit even with things like Spring JDBC to help smooth things over.

Some IDEs, like NetBeans and Eclipse, can even auto-generate models and controllers for you, though I've never been very happy with the results (particularly the failure to use a parent-class and generic methods and the lack of any sort of useful error handling).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.