Java and Android Programming Tutorials
Servlet
Dynamic Login 👤 Example using Servlet in Java
DynamicLogin.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Static Login</title>
<!-- www.raviroza.com -->
</head>
<body>
<h1>Static Login Information</h1>
<hr>
<form action="dlogin" method="post">
<p>
Username <input type="text" name="txtuser">
</p>
<p>
Password <input type="password" name="txtpass">
</p>
<p>
<input type="submit" value="Login" />
</p>
</form>
</body>
</html>
DynamicLoginServlet.java
package RROExamples;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/dlogin")
public class DynamicLoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//create table tbluser (username varchar2(30), password varchar2(30));
//response.getWriter().append("Served at: ").append(request.getContextPath());
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String user = request.getParameter("txtuser");
String pass = request.getParameter("txtpass");
String QRY = "select * from tbluser where ";
QRY += " username = '"+ user +"' and ";
QRY += " password = '"+ pass +"' ";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","hjd");
if(conn.createStatement().executeQuery(QRY).next())
{
pw.print("Welcome, Master : "+user);
response.sendRedirect("home.html");
}
else
{
pw.print("Sorry, My Lord ");
pw.println("<br><a href='signup.html'> Sign Up Here </a> ");
}
}
catch(Exception e)
{
pw.print("Error : "+e.toString());
}
}
}
Watch “Servlet Part-24 | Login and Logout using Http Session & JDBC (Gujarati)” on YouTube
Servlet #SignUp #JDBC #MySql #Eclipse #MySqlWorkBench #ApacheTomcat #Jdk #Java #DynamicLogin #Gujarati #RaviROza #CreateReadUpdateAndDelete #CRUD #Create #read #update #delete #LogIn #LogOut #SignIn # SignOut
Steps to perform the example:
- Login
- Define HttpSession when user info. (id/password) is validated
- Add attributes to newly created session object
- Logout
- Define a servlet for user to Logout
- Retrieve current session object
- Remove attributes from session
- Call invalidate() method on session object
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
Watch “Servlet Part-23 | New User Registration in Servlet using JDBC (Gujarati)” on YouTube
#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
Watch “Servlet Part-22 | Dynamic Login in Servlet using JDBC” on YouTube
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
Watch “Servlet Part 21 | CRUD operation in Servlet Using JDBC (Gujarati)” on YouTube
#CRUD #Servlet #JDBC #MySql #Eclipse #MySqlWorkbench #ApacheTomcat #Jdk #Java
This video features the CRUD operation using JDBC via servlet only, no JSP page is used to perform any database operation.
CRUD is Create, Read, Update & Delete.
Index.html
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Servlet with JDBC</title> </head> <body> <form action="AddEmp" method="post"> <h1> Employee Management</h1> <p> Enter Emp. No : <input type="text" name="txtEno"> </p> <p> Enter Emp. Name : <input type="text" name="txtEname"> </p> <p> Enter Emp. Salary : <input type="text" name="txtEsalary"> </p> <p> DB Operation : <select name="op"> <option value="1">Add</option> <option value="2">Update</option> <option value="3">Delete</option> <option value="4">Display</option> </select> </p> <p> <input type="submit" value="Submit"> </p> </form> </body> </html>
AddEmp.java
package servletWithJDBC; 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("/AddEmp") public class AddEmp extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter pw = response.getWriter(); String op = request.getParameter("op"); Connection conn; PreparedStatement pst=null; String url,username,password,qry=""; url = "jdbc:mysql://localhost:3306/raviroza"; username="root"; password="ravi"; int eno; String ename=""; float esalary; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url,username,password); if(op.equals("1")) { eno = Integer.parseInt(request.getParameter("txtEno")); ename = request.getParameter("txtEname"); esalary = Float.parseFloat(request.getParameter("txtEsalary")); qry = "insert into emp values (?,?,?)"; pst = conn.prepareStatement(qry); pst.setInt(1, eno); pst.setString(2, ename); pst.setFloat(3, esalary); pst.executeUpdate(); } else if(op.equals("2")) { eno = Integer.parseInt(request.getParameter("txtEno")); ename = request.getParameter("txtEname"); esalary = Float.parseFloat(request.getParameter("txtEsalary")); qry = "update emp set ename=?,esalary=? where eno=?"; pst = conn.prepareStatement(qry); pst.setString(1, ename); pst.setFloat(2, esalary); pst.setInt(3, eno); pst.executeUpdate(); } else if(op.equals("3")) { eno = Integer.parseInt(request.getParameter("txtEno")); qry = "delete from emp where eno=?"; pst = conn.prepareStatement(qry); pst.setInt(1, eno); pst.executeUpdate(); } qry = "select * From Emp Order by eno desc"; ResultSet rs = conn.prepareStatement(qry).executeQuery(); pw.println("<table border=1>"); pw.println("<th>Eno.</th>"); pw.println("<th>Emp. Name</th>"); pw.println("<th>Emp. Salary</th>"); while(rs.next()) { pw.println("<tr>"); pw.println("<td>"+rs.getString("eno")+"</td>"); pw.println("<td>"+rs.getString("ename")+"</td>"); pw.println("<td>"+rs.getString("esalary")+"</td>"); pw.println("</tr>"); } pw.println("<h2> Employee Info. updated Successfully ! </h2>"); pw.println("<a href='index.html'>Back</a>"); pw.close(); pst.close(); conn.close(); } catch (ClassNotFoundException | SQLException e) { pw.println("<h2> Driver|SQL Error "+e.toString() +"</h2>"); } } }
Overall process of example.
It is generally the table operation to create, access, manipulate, and delete the data from a database table.
Here, only a single Servlet is performing all the CRUD operation. Operation is decided by the client by selecting a proper option from list of choices.
When a client/user select the “Add” option, the internal value of that option is checked to perform the add operation, like wise when user/client select the “Update” operation the record is updated with given details.
If user/client decide to delete the record the valid number is given and the delete operation is carried.
Finally, if user decides to display all the record there is an option called “Display” to view all the details of the 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
Watch “Servlet Part-16 | How to use Cookies in Servlet (Gujarati)” on YouTube
Video features introduction to Session management.
To track the user session various session tracking approaches are there in servlet.
Here, the Cookies is discussed with suitable example. Index page is used to read user’s name from the client, at the server side there are three servlets. First servlet receives the input/request from the index page, then the session information added using Cookies, then the second servlet also adds the session information in the second servlet using Cookies, and finally third servlet gets the client session information from second servlet.
OS : Windows 10
Jdk : Version 8
IDE : Eclipse Mars
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
Watch “Servlet Part-20 | Servlet with JDBC (Gujarati)” on YouTube
#CRUD #Servlet #JDBC #MySql #Eclipse #MySqlWorkbench #ApacheTomcat #Jdk #Java
This video contains the steps and demonstration to define servlet with JDBC using eclipse to connect with MySql database.
Steps to create it.
- Create a dynamic web project in eclipse
- Configure build path to add database driver (jar files)
- Add dependency (database jar file) to projects lib folder
- Create in html/jsp page for client
- Create a servlet to handle client request
- Define JDBC steps to handle database operation in servlet
Project contains JDBC operation such as loading driver, creating connection and adding employee details using prepared statement.
Subscribe channel to get notification of my next video related to database operation such as update, delete, and select.
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
Watch “Servlet Part 19 | How to use HttpSession in Servlet – Revised (Gujarati)” on YouTube
Video features introduction to Session management.
To track the user session various session tracking approaches are there in servlet.
Here, the HttpSession is discussed with suitable example.
Index page is used for multiple purpose, here i have define two forms which are referring to same form, but with different method type. First form is referring to doGet method whereas later referring to doPost methods.
With doGet method session is defined and doPost method is used to retrieve session the session and its attributes.
Following are the list of files used in the example (index.html, LoginServlet.java).
OS : Windows 10
Jdk : Version 8
IDE : Eclipse Mars
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
Watch “Servlet Part-18 | Session Cookies in servlet revised (Gujarati)” on YouTube
Here, the Cookies are added in response object, two cookies with static values are set and the one cookie is set with value from Index page.
index page is used to read user’s name from the client, at the server side there are three servlets. First servlet receives the input/request from the index page, then the session information added using Cookies, then the second servlet also adds the session information in the second servlet using Cookies, and finally third servlet gets the client session information from second servlet.
Video features introduction to Session management.
To track the user session various session tracking approaches are there in servlet.
OS : Windows 10
Jdk : Version 8
IDE : Eclipse Mars
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