Knowee
Questions
Features
Study Tools

0.(Complusory) Create three servlets, namely CreditServlet.java, DebitServlet.java, andCheckBalanceServlet.java, so that a user accessing ATM.html can credit money, debit money, andcheck the balance of his/her account. Note that the servlets need to share a session-based object tokeep track of the balance in the account.1. After clicking “Check Balance” 2. After crediting $1003. After debiting $1000

Question

0.(Complusory) Create three servlets, namely CreditServlet.java, DebitServlet.java, andCheckBalanceServlet.java, so that a user accessing ATM.html can credit money, debit money, andcheck the balance of his/her account. Note that the servlets need to share a session-based object tokeep track of the balance in the account.1. After clicking “Check Balance” 2. After crediting 1003.Afterdebiting1003. After debiting 1000

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a step-by-step guide on how to create the servlets:

  1. Create the HTML file (ATM.html):
<!DOCTYPE html>
<html>
<body>
<form action="CreditServlet" method="post">
    Credit Amount: <input type="text" name="creditAmount" />
    <input type="submit" value="Credit" />
</form>

<form action="DebitServlet" method="post">
    Debit Amount: <input type="text" name="debitAmount" />
    <input type="submit" value="Debit" />
</form>

<form action="CheckBalanceServlet" method="post">
    <input type="submit" value="Check Balance" />
</form>
</body>
</html>
  1. Create the CreditServlet.java:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class CreditServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Integer balance = (Integer) session.getAttribute("balance");
        if (balance == null) {
            balance = 0;
        }
        String creditAmount = request.getParameter("creditAmount");
        balance += Integer.parseInt(creditAmount);
        session.setAttribute("balance", balance);
        response.sendRedirect("ATM.html");
    }
}
  1. Create the DebitServlet.java:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class DebitServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Integer balance = (Integer) session.getAttribute("balance");
        if (balance == null) {
            balance = 0;
        }
        String debitAmount = request.getParameter("debitAmount");
        balance -= Integer.parseInt(debitAmount);
        session.setAttribute("balance", balance);
        response.sendRedirect("ATM.html");
    }
}
  1. Create the CheckBalanceServlet.java:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class CheckBalanceServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Integer balance = (Integer) session.getAttribute("balance");
        if (balance == null) {
            balance = 0;
        }
        PrintWriter out = response.getWriter();
        out.println("Your current balance is: " + balance);
    }
}
  1. Compile and run the servlets: You need to compile these servlets using javac and run them on a servlet container like Apache Tomcat.

Please note that this is a basic example and doesn't include any error handling or security measures. In a real-world application, you would need to add those.

This problem has been solved

Similar Questions

Write a java program to create a bank account with Name of the account holder, type of account(savings or  current), Account number and Balance amount in the account. Also, create 5 customers for the class Bank_account using array of objects. Access the customers to perform credit, debit and display of balance.Input 1:Enter the customers:ReenaSavings1231000RobinCurrent4562000PraveenSavings7893000SherinCurrent2342500DeenaSavings5672000Enter choice:1. Credit2. Debit Output :ReenaSavings1232000

Write a java program to create a bank account with Name of the account holder, type of account(savings or current), Account number and Balance amount in the account. Also, create 5 customers for the class Bank_account using array of objects. Access the customers to perform credit, debit and display of balance. Input 1: Enter the customers: Reena Savings 123 1000 Robin Current 456 2000 Praveen Savings 789 3000 Sherin Current 234 2500 Deena Savings 567 2000 Enter choice: 1. Credit 2. Debit

Write a java program to create a bank account with Name of the account holder, type of account(savings or  current), Account number and Balance amount in the account. Also, create 5 customers for the class Bank_account using array of objects. Access the customers to perform credit, debit and display of balance.Input 1:Enter the customers:ReenaSavings1231000RobinCurrent4562000PraveenSavings7893000SherinCurrent2342500DeenaSavings5672000Enter choice:1. Credit2. Debit

A trial balance will not balance if:a.a correct journal entry is posted twice.b.the purchase of supplies on account is debited to Supplies and credited to Cash.c.a $100 cash drawing by the owner is debited to Owner’s Drawings for $1,000 and credited to Cash for $100.d.a $450 payment on account is debited to Accounts Payable for $45 and credited to Cash for $45

What if the ATM.html is the following:<form action="debit" method="POST"> <p><strong>Please select the amount that you want to withdraw from your account?</strong></p> <dl> <dd><input type="radio" name="Amount" value="1">1 </dd> <dd><input type="radio" name="Amount" value="10">10</dd> <dd><input type="radio" checked name="Amount" value="100">100</dd> <dd><input type="radio" name="Amount" value="1000">1000</dd> </dl> <p><input type="submit" value="Submit"> </p> </form> <form method="POST" action="credit"> <p><strong>Please select the amount that you want to deposit in your account?</strong></p> <dl> <dd><input type="radio" name="Amount" value="1">1 </dd> <dd><input type="radio" name="Amount" value="10">10</dd> <dd><input type="radio" checked name="Amount" value="100">100</dd> <dd><input type="radio" name="Amount" value="1000">1000</dd> </dl> <p><input type="submit" value="Submit" name="B1"></p> </form> <form method="GET" action="checkBalance"> <p><input type="submit" value="Check Balance" name="B1"></p> </form>

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.