MVC with database[Registration Form with database] in JSP

In this tutorial I have given clarity about the concept of MVC with database by taking a suitable example. Let’s see :

Here we have a registration form(reg.jsp file) containing four attributes like Name, Contact, Email, and Password.

We will develop application by implementing concept of MVC with database.

Based on the registration form we will create a table(named as register table in registrationdb database in MySQL envrionment).

Create table registrationdb.register
(
    Name varchar(200),
    Contact varchar(200),
    Email varchar(200) primary key,
    Password varchar(200)
)
img

Required Files that we have to develop:

reg.jsp --> client end
RegisterBean.java & RegisterDao.java --> model component
RegisterServlet.java -> controller component
Welcome.jsp & error.jsp -> view component
web.xml -> deployment descriptor file


img
reg.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Registration Form</title>
    </head>
    <body>
        <center><h1>Registration Form</h1>
            <form action="RegisterServlet">
                <table>
                    <tr>
                        <td>Name</td>
                        <td><input type="text"name="name"></td>
                    </tr>
                    <tr>
                        <td>Contact</td>
                        <td><input type="text"name="contact"></td>
                    </tr>
                    <tr>
                        <td>Email</td>
                        <td><input type="email"name="email"></td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td><input type="password"name="pass"></td>
                    </tr>
                    <tr>
                        <td><input type="submit"value="register"></td>
                    </tr>
                </table>
            </form>
        </center>
    </body>
</html>

RegisterBean.java
package com.silan;
public class RegisterBean {
    private String name, contact, email, password;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getContact() {
        return contact;
    }
    public void setContact(String contact) {
        this.contact = contact;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

RegisterDao.java
package com.silan;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class RegisterDao {
    public String UserRegistration(RegisterBean registerBean) throws
    Exception
    {
        String s1=registerBean.getName();
        String s2=registerBean.getContact();
        String s3=registerBean.getEmail();
        String s4=registerBean.getPassword();
        int k=0;
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            Connection

            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/registrationdb
            ","root","Silan@123");

            PreparedStatement ps=con.prepareStatement("insert into

            register values(?,?,?,?)");

            ps.setString(1, s1);
            ps.setString(2, s2);
            ps.setString(3, s3);
            ps.setString(4, s4);
            k=ps.executeUpdate();
            if(k>0)
            {
                return "success";
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return "something is wrong";
    }
}

welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <%
        out.print("registration is successful");
        %>
    </body>
</html>

error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <%
        out.print("sry! invalid credential");
        %>
    </body>
</html>

Output:
img

then we will go to register table inside registrationdb database and check whether the record have inserted or not !

img

Output

img

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext