0% found this document useful (0 votes)
19 views3 pages

Wtexp 10

The document outlines the design and implementation of a web application for banking transactions using EJB, featuring deposit and withdrawal functionalities. It includes code snippets for the JSP page, EJB interfaces, and servlet handling the transactions. The application allows users to input amounts for transactions and displays the new balance or error messages accordingly.

Uploaded by

bargeanjali650
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Wtexp 10

The document outlines the design and implementation of a web application for banking transactions using EJB, featuring deposit and withdrawal functionalities. It includes code snippets for the JSP page, EJB interfaces, and servlet handling the transactions. The application allows users to input amounts for transactions and displays the new balance or error messages accordingly.

Uploaded by

bargeanjali650
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment No: 10

Aim: Design and implement a business interface with necessary business logic for any web
application using EJB. e.g. Design and implement the web application logic for deposit and
withdraw amount transactions using EJB.
Code:
bank.jsp:
<%@ page language="java" %>
<!DOCTYPE html>
<html>
<head>
<title>Banking Page</title>
</head>
<body>
<h2>Bank Transaction</h2>
<form method="post" action="bank">
<label>Amount:</label>
<input type="number" step="0.01" name="amount" required />
<br><br>
<button type="submit" name="action" value="deposit">Deposit</button>
<button type="submit" name="action" value="withdraw">Withdraw</button>
</form>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------------------
BankService.java:
package ejb;

import jakarta.ejb.Remote;

@Remote
public interface BankService {
double deposit(double amount);
double withdraw(double amount);
double getBalance();
}
-------------------------------------------------------------------------------------------------------------------------------------------
BankServiceBean.java:
package ejb;
import jakarta.ejb.Stateless;
@Stateless
public class BankServiceBean implements BankService {
private double balance = 0;

@Override
public double deposit(double amount) {
balance += amount;
return balance;
}
@Override
public double withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
throw new RuntimeException("Insufficient balance.");
}
return balance;
}
@Override
public double getBalance() {
return balance;
}
}
------------------------------------------------------------------------------------------------------------------------------
BankServlet.jav:
package servlet;

import ejb.BankService;
import jakarta.ejb.EJB;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

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

@WebServlet("/bank")
public class BankServlet extends HttpServlet {

@EJB
private BankService bankService;

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws IOException {

String action = request.getParameter("action");


double amount = Double.parseDouble(request.getParameter("amount"));
double newBalance;

try {
if ("deposit".equals(action)) {
newBalance = bankService.deposit(amount);
} else {
newBalance = bankService.withdraw(amount);
}

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h2>Transaction Successful!</h2>");
out.println("<p>New Balance: $" + newBalance + "</p>");
} catch (Exception e) {
response.getWriter().println("<h3>Error: " + e.getMessage() + "</h3>");
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------
Web.xml:
<!-- WebContent/WEB-INF/web.xml -->
<web-app>
<servlet>
<servlet-name>BankServlet</servlet-name>
<servlet-class>servlet.BankServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BankServlet</servlet-name>
<url-pattern>/bank</url-pattern>
</servlet-mapping>
</web-app>
-------------------------------------------------------------------------------------------------------------------------------------------
Output:

Fig.1 Home page

Fig.2 Deposit Transaction Done

Fig.3 Withdraw Transaction Done

You might also like