Friday 30 June 2017

Jsp-Servlet App-2

BANK APPLICATION


This is part-2 of Bank application.Click on below link to follow part-1

https://programmersthing.blogspot.in/2017/06/jsp-servlet-app-1.html



11.Passbook.jsp Page :

package com.techlabs.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.RequestDispatcher;
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 com.mysql.jdbc.Connection;

/**
* Servlet implementation class Passbook
*/
@WebServlet("/Passbook")
public class Passbook extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Passbook() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher requestDispatcher = request.getRequestDispatcher("passbook.jsp");
requestDispatcher.forward(request, response);
Connection connection = (Connection) RegisterCon.connectDb();
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
try {
String sql = "select * from master";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet set = statement.executeQuery();
request.setAttribute("data", set);
//requestDispatcher.forward(request, response);

String str = "<table><tr><th>Cid</th><th>UserName</th><th>Password</th><th>Balance</th><th>Email</th></tr>";
while (set.next()) {
str += "<tr><td>" + set.getString(1) + "</td><td>" + set.getString(2) + "</td>" + "<td>"
+ set.getString(3) + "</td><td>" + set.getString(4) + "</td>" + "<td>" + set.getString(5)
+ "</td></tr>";
}
str += "</table>";
writer.println(str);
connection.close();

} catch (Exception e) {
System.out.println(e);
}

}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

}

}

12.profile.jsp page :
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!doctype html>
<html lang="en">

<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
crossorigin="anonymous">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
<style>
body {
background-image:
url("http://wallpaper-gallery.net/images/website-wallpaper/website-wallpaper-11.jpg");
background-color: #cccccc;
text-align: center;
}

button {
margin: 100px;
display: inline;
height: 50px;
width: 200px;
}

header, footer {
padding: 1em;
color: white;
background-color: black;
clear: left;
text-align: center;
}
</style>

<meta charset="utf-8">
<meta name="Homepage" content="Starting page for the survey website ">

<title>Bank App</title>
<style>
nav {
float: left;
max-width: 200px;
margin: 20;
padding: 10;
}

nav ul {
list-style-type: none;
padding: 0;
}

nav ul a {
text-decoration: none;
}

ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: gray;
}

li {
float: left;
}

li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

li a:hover {
background-color: #111;
}
</style>
</head>

<body>
<%
if (session.getAttribute("username") == null) {
response.sendRedirect("/bank-app");
}
//String pic = (String) session.getAttribute("pic");
%>
<header>
<h1>Welcome To Profile</h1>
<h3>
<marquee style="color: aliceblue">Now pay your bills using
Bharat Bill Payment services on ABC Mobile Banking & Internet
Banking</marquee>
</h3>
</header>
<nav>
<ul class="nav nav-pills">
<li><a href="/bank-app/Home"><span
class="glyphicon glyphicon-user">Home</a></li>
<br>
<br>
<br>
<li><a href="/bank-app/Transaction"><span
class="glyphicon glyphicon-user">Transaction</a></li>
<br>
<br>
<br>
<li><a href="/bank-app/Passbook"><span
class="glyphicon glyphicon-list">Passbook Details</a></li>
<br>
<br>
<br>
<li><a href="/bank-app/Profile"><span
class="glyphicon glyphicon-list">My Profile</a></li>
<br>
<br>
<br>
<li><a href="/bank-app/Logout"><span
class="glyphicon glyphicon-list">Logout</a></li>
</ul>
</nav>
<h2></h2>
<div></div>
<article>
</article>

</body>

</html>

13.Profile.jsp servlet :

import java.io.IOException;
import java.io.InputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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 javax.servlet.http.HttpSession;

import com.mysql.jdbc.Connection;

/**
* Servlet implementation class Profile
*/
@WebServlet("/Profile")
public class Profile extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Profile() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection connection = (Connection) RegisterCon.connectDb();
HttpSession session = request.getSession();
String Cid = (String) session.getAttribute("cid");
try {
PreparedStatement statement = connection
.prepareStatement("SELECT Image FROM master WHERE Cid = '" + Cid + "'");
ResultSet resultSet = statement.executeQuery();
String imgLen = "";
if (resultSet.next()) {
imgLen = resultSet.getString(6);
System.out.println(imgLen.length());
}
resultSet = statement.executeQuery("select Image from master where Cid = '" + Cid + "'");
if (resultSet.next()) {
int len = imgLen.length();
byte[] rb = new byte[len];
InputStream readImg = resultSet.getBinaryStream(6);
int index = readImg.read(rb, 0, len);
System.out.println("index" + index);

statement.close();
response.reset();
response.setContentType("image/jpg");
response.getOutputStream().write(rb, 0, len);
response.getOutputStream().flush();

}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

request.getRequestDispatcher("profile.jsp").forward(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
14.  result.jsp Page :
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<h3><%=request.getAttribute("Message")%></h3>
</center>
</body>
</html>
15.Logout.java Servlet :

import java.io.IOException;
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 javax.servlet.http.HttpSession;

/**
* Servlet implementation class Logout
*/
@WebServlet("/Logout")
public class Logout extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Logout() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.removeAttribute("username");
session.invalidate();
response.sendRedirect("/bank-app");
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}

16.Diposite.java Servlet :
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;

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 javax.servlet.http.HttpSession;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
/**
* Servlet implementation class Diposite
*/
@WebServlet("/Diposite")
public class Diposite extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Diposite() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
double diposite = Double.valueOf(request.getParameter("dmoney"));
final String type = "D";
HttpSession session = request.getSession();
String Cid = (String) session.getAttribute("cid");
try {
Connection connection = (Connection) RegisterCon.connectDb();
System.out.println(Cid);
try {
connection.setAutoCommit(false);
PreparedStatement statement = connection
.prepareStatement("insert into transaction(Cid,Type,Amount) values ('" + Cid + "','" + type
+ "','" + diposite + "')");
PreparedStatement statement2 = connection.prepareStatement(
"update master set Balance = Balance + '" + diposite + "' where Cid = '" + Cid + "'");
// statement.setString(2, Cid);
// statement.setString(3, type);
// statement.setDouble(4, withdraw);
// statement.setTimestamp(5, new
// Timestamp(System.currentTimeMillis()));

// statement2.setString(1, Cid);
// statement2.setDouble(4, withdraw);

statement.executeUpdate();
statement2.executeUpdate();
connection.commit();
JOptionPane optionPane = new JOptionPane("Transaction Done", JOptionPane.WARNING_MESSAGE);
JDialog dialog = optionPane.createDialog("Warning!");
dialog.setAlwaysOnTop(true); // to show top of all other
// application
dialog.setVisible(true);
double balance = (double) session.getAttribute("balance") + diposite;
session.setAttribute("balance", balance);
request.getRequestDispatcher("transaction.jsp").forward(request, response);

} catch (SQLException exception) {
connection.rollback();
JOptionPane optionPane = new JOptionPane("Error in Transaction", JOptionPane.WARNING_MESSAGE);
JDialog dialog = optionPane.createDialog("Warning!");
dialog.setAlwaysOnTop(true); // to show top of all ot
// application
dialog.setVisible(true);
request.getRequestDispatcher("transaction.jsp").forward(request, response);
// TODO: handle exception
}

} catch (SQLException ex) {

System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
// TODO: handle exception
}

}

}
17.Withdraw.java Servlet :
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;

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 javax.servlet.http.HttpSession;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
* Servlet implementation class Withdraw
*/
@WebServlet("/Withdraw")
public class Withdraw extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Withdraw() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
double withdraw = Double.valueOf(request.getParameter("wmoney"));
final String type = "W";
HttpSession session = request.getSession();
String Cid = (String) session.getAttribute("cid");
try {
Connection connection = (Connection) RegisterCon.connectDb();
System.out.println(Cid);
try {
connection.setAutoCommit(false);
PreparedStatement statement = connection
.prepareStatement("insert into transaction(Cid,Type,Amount) values ('"+Cid+"','"+type+"','"+withdraw+"')");
PreparedStatement statement2 = connection
.prepareStatement("update master set Balance = Balance - '"+withdraw+"' where Cid = '"+Cid+"'");
//statement.setString(2, Cid);
//statement.setString(3, type);
//statement.setDouble(4, withdraw);
//statement.setTimestamp(5, new Timestamp(System.currentTimeMillis()));

//statement2.setString(1, Cid);
//statement2.setDouble(4, withdraw);

statement.executeUpdate();
statement2.executeUpdate();
connection.commit();
JOptionPane optionPane = new JOptionPane("Transaction Done", JOptionPane.WARNING_MESSAGE);
JDialog dialog = optionPane.createDialog("Warning!");
dialog.setAlwaysOnTop(true); // to show top of all other
// application
dialog.setVisible(true);
double balance = (double) session.getAttribute("balance") - withdraw;
session.setAttribute("balance", balance);
request.getRequestDispatcher("transaction.jsp").forward(request, response);

} catch (SQLException exception) {
connection.rollback();
JOptionPane optionPane = new JOptionPane("Error in Transaction", JOptionPane.WARNING_MESSAGE);
JDialog dialog = optionPane.createDialog("Warning!");
dialog.setAlwaysOnTop(true); // to show top of all ot
// application
dialog.setVisible(true);
request.getRequestDispatcher("transaction.jsp").forward(request, response);
// TODO: handle exception
}

} catch (SQLException ex) {

System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
// TODO: handle exception
}

}

}
18.Mailser.java Class:

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mailer {

public static  void send(String from,String password,String to,String sub,String msg){
//Get properties object
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
//get Session
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from,password);
}
});
//compose message
try {
MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(sub);
message.setText(msg);
//send message
Transport.send(message);
System.out.println("message sent successfully");
} catch (MessagingException e) {throw new RuntimeException(e);}

}

}
19. ErrorServiece.java Class :
import javax.swing.JOptionPane;

public class ErrorService {
public void msgbox(String s){
JOptionPane.showMessageDialog(null, s);
}

}


20.RegisterCon.java Class :

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class RegisterCon {

public static Connection connectDb() {
Connection connection = null;
try {

Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost/bank?" + "user=root&password=admin");

} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
return connection;

}
}




21.OUTPUT


Index Page




Login Page


Transaction Page


Passbook Details

                         




Spring-App

Application In Spring Spring Framework is an open source Java Platform that provides comprehensive infrastructure support for developi...