VIDEO
#Servlet #SignUp #JDBC #MySql #Eclipse #MySqlWorkBench #ApacheTomcat #Jdk #Java #DynamicLogin #Gujarati #RaviROza
This video features the Sign up operation (New User registration) using JDBC via servlet only, no JSP page is used to perform any database operation.
MySql database is used as a back end support, the table tbl_users has been created using MySql Workbench in raviroza database.
Create the following files
NewUser.html (new user registration form – client side)
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>User Registration</title>
</head>
<body>
<form action="Signup" method="post">
<h1>New User Registration (Sign up)</h1>
<hr>
<p> Username : <input type="text" name="txtUsername"> </p>
<p> Password : <input type="password" name="txtPassword"> </p>
<hr>
<p> <input type="submit" value="Sign Up">
<input type="reset" value="Reset">
</p>
</form>
</body>
</html>
SignUp.java (servlet to add new user details in database if user doesn’t exist)
package dynamicLogin;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
@WebServlet("/Signup")
public class Signup extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter pw = response.getWriter();
response.setContentType("text/html");
Connection conn;
PreparedStatement pst;
String url,unam,pass,QRY="";
url = "jdbc:mysql://localhost:3306/raviroza";
unam = "root";
pass = "ravi";
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,unam,pass);
String username = request.getParameter("txtUsername");
String password = request.getParameter("txtPassword");
QRY = "Select Username from tbl_users where Username=? and password=?";
pst = conn.prepareStatement(QRY);
pst.setString(1, username);
pst.setString(2, password);
if(pst.executeQuery().next())
{
pw.println("<h1>User Already Exist !!!</h1>");
}
else
{
QRY = "Insert into tbl_users values (?, ?)";
pst = conn.prepareStatement(QRY);
pst.setString(1, username);
pst.setString(2, password);
pst.executeUpdate();
pw.println("<h1>User Registration Done </h1>");
pw.println("<p> <a href='index.html'>Login Here </a> </p>");
}
pw.close();
pst.close();
conn.close();
}
catch(Exception e)
{
pw.println("DB Error : "+e.toString());
}
}
}
Overall process of example: When a new user select the “Sign Up” option from the registration page the request is sent to the SignUP Servlet, the servlet read the values of username and password from request. The new user registration module has been linked with Login module.
Servlet defines the Database connection with valid database connection information.
Prepared statement is used to check the username and password exists in users table. then, the same prepared statement is used to add new user information in database.
OS : Windows 10 Jdk : Version 8 IDE : Eclipse Mars Database : MySQL using MySql Workbench 5.2 CE Server : Apache Tomcat 7
follow me @https://raviroza.wordpress.com/ https://twitter.com/raviozaIT https://www.facebook.com/ravi.oza.it
Subscribe my channel to get latest video notification https://www.youtube.com/user/ravioza101
Like this: Like Loading...