The Banking application project is developed by Servlet and MySQL. The project is built to manage money deposit transactions. enter the account number along with the pin no . If the pin no is correct user shall be able to deposit the amount. This kind of simple example will help you to learn java web development easy way to understand.lets moving to the code.
First step Design the login Form
Create the file Index.html paste the following code.
<html> <head> <title>TODO supply a title</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> </head> <body> <div class="container"> <div class="row"> <div> <form action="servlet1" method="POST"> <div class="form-group"> <h2>Welcome to Ebank</h2> </div> <div class="form-group"> <label>Enter the Account Number</label> <input type="text" name="accno" class="form-control" placeholder="AccNo"> </div> <div class="form-group"> <label>Pin No</label> <input type="password" name="pinno" class="form-control" placeholder="PinNo"> </div> <div class="form-group"> <button type="submit" class="btn btn-success">Submit</button> </div> </form> </div> </div> </div> </body> </html>
After created the login form we have to check the validation stuff. If the account and pin no both matched successfully visiting to the account holder account able to enter the deposit amount on the textbox and click submit button. message will be displayed account deposited successfully.
servlet1.java
import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; 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("/servlet1") public class servlet1 extends HttpServlet { PrintWriter out = null; Connection con; PreparedStatement pst; ResultSet rs; @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String result; Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost/bar", "root", ""); ServletContext context = getServletContext(); context.setAttribute("accno", ""); String accno = request.getParameter("accno"); String pinno = request.getParameter("pinno"); pst = con.prepareStatement("select * from login where accno = ? and pinno = ?"); pst.setString(1, accno); pst.setString(2, pinno); rs = pst.executeQuery(); boolean row = false; row = rs.next(); if(row == true) { result = rs.getString(2); context.setAttribute("accno", result); RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/secondservlet"); if(dispatcher == null) { } dispatcher.forward(request, response); con.close(); } else { out = response.getWriter(); response.setContentType("text/html"); out.println("<html>"); out.println("<body bgcolor=pink>"); out.println("Please check the Accno and Balance"); out.println("</body"); out.println("</html"); out.close(); } } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } } }
secondservlet.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; 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("/secondservlet") public class secondservlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); response.setContentType("text/html"); ServletContext context = getServletContext(); Object obj = context.getAttribute("accno"); String value = obj.toString(); out.println("<html>"); out.println("<body bgcolor=pink>"); out.println("<center>"); out.println("<h2>Ebank</h2>"); out.println("<center>"); out.println("<Form method=post action=servlet3>"); out.println("<b> Click the Deposit button</b>"); out.println("<table>"); out.println("<tr>"); out.println("<td>"); out.println("Account Number : " + value); out.println("</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>"); out.println("Deposit Amount : </td> <td> <input type=text name=amount value=0>"); out.println("</td>"); out.println("</tr>"); out.println("</table>"); out.println("<input type=submit value=deposit>"); out.println("</br>"); out.println("</Form>"); out.println("</body>"); out.println("</html>"); } }
servlet3.java
import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Locale; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletContext; 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("/servlet3") public class servlet3 extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { Connection con; PreparedStatement pst; ResultSet rs; PrintWriter out = response.getWriter(); response.setContentType("text/html"); Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost/bar", "root", ""); ServletContext context = getServletContext(); Object obj = context.getAttribute("accno"); String accno = obj.toString(); DateTimeFormatter df = DateTimeFormatter.ofPattern("yyy/MM/dd"); LocalDateTime now = LocalDateTime.now(); String date = df.format(now); String amount = request.getParameter("amount"); pst = con.prepareStatement("insert into account_holder(accnum,date,mdeposit)values(?,?,?)"); pst.setString(1, accno); pst.setString(2, date); pst.setString(3, amount); int rows = pst.executeUpdate(); if(rows==1) { out.println("Your Transaction have been done"); } else { out.println("Your Transaction failed"); } } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } } }
i have attached the video link below. which will do this tutorials step by step.