How to login and register with JSP and Servlet using the MYSQL database. In this Java web project tutorial example, we are going to see login and Registration activity with JSP and servlet and MYSQL database.
Login and Registration with JSP and servlet with Mysql
- Create a user table into the MySQL database.
- Create a simple Java web project in eclipse using maven or dynamic project.
- Create views in Jsp for the login and registration page.
- Create a controller using a servlet that will handle requests and responses.
Here is complete video tutorial to perform login and registration with JSP and MySQL with practices that will show you, how to create java application and connect it with MSQL a database. or an idea for java database connectivity(JDBC) with MySQL so please watch the complete video first.
Tools and Technology to create a Java web project
Database: MySQL workbench
Language : HTML or JSP,servlet
Server: Glassfish or Tomcat
Let’s start the java database connectivity(JDBC) with MySQL.
Configure your MYSQL database
Create a new database with the name “mydb” and table name “user”. also, remember your database connection URL, User Name, and Password. that will be required at the time of database connectivity with Java.
CREATE TABLE `mydb`.`user` ( `uid` INT NOT NULL AUTO_INCREMENT, `uname` VARCHAR(45) NULL, `upass` VARCHAR(45) NULL, `email` VARCHAR(45) NULL, PRIMARY KEY (`uid`));
Create MYSQL Database connection with Java
Create a class name as “MyDb.java” that will be responsible to create a database connection with MySQL. Below is the code for “MyDb.java”. this class contains method “getCon()” that will return a MySQL connection.
Steps to create database connection with Java
- Define a Connection variable
public Connection con;
- Load MYSQL driver
Class.forName("com.mysql.jdbc.Driver");
- Create a connection with MYSQL
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class MyDb { public Connection con; public Connection getCon(){ try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; }}
Create a registration form in JSP
Create a JSP or HTML file with the name “registration.jsp”. this file contains HTML elements and an HTML form to design a registration form and when the user clicks on the registration button the request will be sent to “Main.java” which is defined into the action attribute of the form.
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <form method="post" action="Main"> <pre> Name :<input name="name" type="text"> Password:<input name="pass" type="text"> Email:<input name="email" type="email"> <input type="submit" value="button"> </pre> </form> </body> </html>
Create a Login form in JSP
Create another HTML form to perform login activity. Let’s create one more JSP or HTML file with the name “login.jsp or login.html”, this view(JSP file) contains two input fields and a login button. when the user enters the user name and password and hits the login button the request will be sent to “Login.java”
<body> <pre> <form method="post" action="Login"> <input type="text" name = "uname"> User Name : <input type="text" name = "upass"> User Pass : <input value="Submit" type="submit"> </form> </pre> </body>
Create a servlet for registraiton form
Create a servlet with the name “Main.java” that will get requests from “registration.jsp” and create an object of MyDb class to create a connection with MySQL and send the data to the MYSQL user table.
String name = request.getParameter("name"); String pass = request.getParameter("pass"); String email = request.getParameter("email"); MyDb db = new MyDb(); Connection con = db.getCon(); try { Statement stmt = con.createStatement(); stmt.executeUpdate("insert into user (uname,upass,email)values('"+name+"','"+pass+"','"+email+"')"); out.println("data inserted sucessfully"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }
Now, the Registration Part is Completed now will code for the login part.
Create a servlet for login form
Create a servlet with the name “Login.java” that will get requests from “login.jsp” and create an object of MyDb class to create a connection with MySQL and check the username and password is available or not in the user table. If the user exists it will be redirected to the welcome page otherwise it will send an error message. “Login is failed”.
String name = request.getParameter("uname"); String pass = request.getParameter("upass"); MyDb db = new MyDb(); Connection con = db.getCon(); try { Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from user where uname = '"+name+"'"); if(rs.next()){ out.println("Login Sucessfully"); } } catch (SQLException ex) { Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex); }
I hope it’s now clear to you. how to create a login and registration page and java database connectivity(JDBC) with MySQL.