Thursday, March 12, 2009

Bank Accouunt Servlet Sample

package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import manager.BankAccountMgr;
import manager.CustomerMgr;
import model.BankAccount;
import model.Customer;

public class BankAccountServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public BankAccountServlet() {
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Servlet starting............");
//try {
retrieveAcctType(request,response);
doPost(request, response);
//} catch (Exception e) {
//e.printStackTrace();
//}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
System.out.println("Servlet starting............do post");
updatebalance(request,response);
}catch (Exception e)
{
e.printStackTrace();
}

/*String action =request.getParameter("action");
System.out.println(action);
if(action.equalsIgnoreCase("transfertoother"))
{
try{
initialTransfertoOther(request,response);
}
catch (Exception e)
{
e.printStackTrace();
}
}*/
//if (request.getParameter("T").equals("Transfer"))
// {
//}
}
public void retrieveAcctType(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException
{
//HttpSession session=request.getSession(true);
//String id=request.getAttribute("userid").toString();
//String id=request.getParameter("roleid");
String id="C1";
try{

BankAccountMgr accountmgr=new BankAccountMgr();
BankAccount[] result=(BankAccount[]) accountmgr.retrieveAcctType(id);
request.setAttribute("acctid",result);

RequestDispatcher dispatcher = request.getRequestDispatcher("TransfertoOther.jsp");
dispatcher.forward(request, response);


}
catch(Exception e){
e.printStackTrace();
request.setAttribute("exception", e);
RequestDispatcher disp=request.getRequestDispatcher("ErrorMsgForm.jsp");
disp.forward(request,response);
}

}

public void updatebalance(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException
{
String custId="C1";
// String id=request.getParameter("id");
String facctid= request.getParameter("fromAccount");
System.out.println(facctid);
String tacctid=request.getParameter("transferToAccountId");
String tamount=request.getParameter("transferAmount");
String accounttype=request.getParameter("atype");
Float tranamount;
Integer fromaccId,toaccId;
int resultvalidate = 0;
BankAccountMgr accountmgr=new BankAccountMgr();
try{

resultvalidate = accountmgr.validateaccount(Integer.parseInt(request.getParameter("transferToAccountId")));}
catch(Exception e)
{
System.out.println(e.getMessage());
}
if (resultvalidate == 1)
{
try{
fromaccId=Integer.parseInt(facctid);
toaccId=Integer.parseInt(tacctid);
tranamount=Float.parseFloat(tamount);
try{
//BankAccountMgr accountmgr=new BankAccountMgr();
boolean result=accountmgr.updatebalance(custId, fromaccId,toaccId,tranamount,accounttype);
if(result ==true)
{
RequestDispatcher disp=request.getRequestDispatcher("TranMsgForm.jsp");
disp.forward(request,response);
}
}
catch(Exception e){
e.printStackTrace();
request.setAttribute("exception", e);
RequestDispatcher disp= request.getRequestDispatcher("ErrorMsgForm.jsp");
disp.forward(request,response);
}
}catch(NumberFormatException nfe)
{
PrintWriter out=response.getWriter();
out.println("error");
}
}
else
{
PrintWriter out=response.getWriter();
out.println("Transfer to account not found");
}
}

/*private void initialTransfertoOther(HttpServletRequest request,HttpServletResponse response)throws Exception{
try
{
//HttpSession session=request.getSession(true);
//String id=request.getAttribute("userid").toString();
String id="C1";
BankAccountMgr accountmgr=new BankAccountMgr();
BankAccount[] result=(BankAccount[]) accountmgr.retrieveAcctType(id);
request.setAttribute("acctid",result);
//BankAccount[] acc=null;
RequestDispatcher dispatcher = request.getRequestDispatcher("TransfertoOther.jsp");
if (dispatcher != null){
dispatcher.forward(request, response);
}
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("GET Request.No Form Post");

}
catch(Exception e){
e.printStackTrace();
request.setAttribute("exception", e);
RequestDispatcher disp=request.getRequestDispatcher("ErrorMsgForm.jsp");
disp.forward(request,response);
}
}*/
}

No comments:

Post a Comment