Servlet #JDBC #MySql #Eclipse #MySqlWorkBench #ApacheTomcat #Jdk #Java #LoginApplication #DynamicLogin #Gujarati #RaviROza
This video features the Login operation 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.
New Web Dynamic project has been used to define login application in eclipse.
Following are the resources used in project.
index.html (to read login information from client)
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Dynamic Login Application</title> </head> <body> <form action="Login" method="post"> <h1>Welcome to Your Domain Login Page</h1> <hr> <p> Username : <input type="text" name="txtUsername"> </p> <p> Password : <input type="password" name="txtPassword"> </p> <hr> <p> <input type="submit" value="Login"> <input type="reset" value="Reset"> </p> </form> </body> </html>
Login.java (controller servlet to validate username and password )
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("/Login") public class Login 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 * 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>Welcome User, "+username+ "</h1>"); pw.println("<p> <a href='Logout'>Logout Here </a> </p>"); } else { pw.println("<h1>Invalid login details </h1>"); } } catch(Exception e) { pw.println("DB Error : "+e.toString()); } } }
Overall process of example:
When a client/user select the “Submit” option from the clients page the request is sent to the Login Servlet, the servlet read the values of username and password from request.
Servlet defines the Database connection with valid database connection information.
Then, prepared statement is used to execute the queries. Here i have use used the prepared statement to check the username and password is exist in users table.
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