0% found this document useful (0 votes)
39 views4 pages

Fall 2023 - CS506 - 2

The document provides instructions for Assignment No. 02 on web design and development. It asks students to create two Java servlets - TransactionServlet and BalanceServlet - to simulate basic banking transactions. TransactionServlet will allow the user to enter an initial balance and deposit or withdrawal amount, perform the transaction, and store the updated balance in the request. It will then forward the request to BalanceServlet. BalanceServlet will retrieve the updated balance from the request, calculate the new balance, and display it along with a success message. Students only need to submit the code for the processRequest() method of each servlet.

Uploaded by

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

Fall 2023 - CS506 - 2

The document provides instructions for Assignment No. 02 on web design and development. It asks students to create two Java servlets - TransactionServlet and BalanceServlet - to simulate basic banking transactions. TransactionServlet will allow the user to enter an initial balance and deposit or withdrawal amount, perform the transaction, and store the updated balance in the request. It will then forward the request to BalanceServlet. BalanceServlet will retrieve the updated balance from the request, calculate the new balance, and display it along with a success message. Students only need to submit the code for the processRequest() method of each servlet.

Uploaded by

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

Assignment No.

02 Student ID: BC220213091


Semester: Fall 2023 Name: Ghullam Mustafa

Web Design and Development CS506

Instructions:
Please read the following instructions carefully before submitting the assignment. It should be clear
that your assignment will not get any credit if:

 The assignment is submitted after due date.


 The submitted assignment does not open or file is corrupt.
 Assignment is copied (partial or full) from any source (websites, forums, students, etc.)
Strict action will be taken in this regard.

Note (Submission Instruction):


You are required to submit only .doc or .docx word file. No need to send complete project code, just
send the code of processRequest() method of each Servlet. Details are given in the assignment
description.

Objectives:
The objective of this assignment is to provide hands-on experience of Java Programming concepts
including:
 Servlet Basics
 Process Request
 Member Functions and main function

For any assignment related query, contact at [email protected]

Covered Lectures: Assignment No.2 is from 26-37 lectures.

Problem Statement: Marks 20


In this assignment, you will create two Java Servlets to perform basic banking transactions. The first
Servlet will simulate a deposit or withdrawal operation, and the second Servlet will calculate the
account balance after the transaction.

1. Transaction Servlet:
o Create a Servlet named TransactionServlet.
o Design an HTML form that allows the user to enter their initial account balance,
specify whether they want to make a deposit or withdrawal, and enter the transaction
amount.
o Perform the deposit or withdrawal operation based on the user's input.
o Store the updated account balance as an attribute in the request.
o Forward the request to BalanceServlet (using the Request Dispatcher method).
2. Balance Servlet:
o Create a Servlet named BalanceServlet.
o Retrieve the updated account balance from the request attribute set by
TransactionServlet.
o Calculate the new account balance after the transaction.
o Display the new balance on the web page, along with a message indicating whether
the transaction was successful.

Note: No need to send complete code, just send the code of processRequest() method of each Servlet.
Details are given below:

Solution:
FirstServlet (TransactionServlet) processRequest() method:
protected void process request(HttpServletRequest request, HttpServletResponse response) throws

ServletException, IOException{

protected void processRequest(HttpServletRequest request, HttpServletResponse


response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
double initialBalance = Double.parseDouble(request.getParameter("Balance"));
String transactionType = request.getParameter("Type");
double transactionAmount = Double.parseDouble(
request.getParameter("Amount"));
double updatedBalance = initialBalance;
if(transactionType.equals("Withdrawal"))
{
updatedBalance -= transactionAmount;
}
else
{
updatedBalance += transactionAmount;
}
request.setAttribute("updatedBalance", updatedBalance);
request.getRequestDispatcher("BalanceServlet"). forward (request, response);
}
} }
SecondServlet (BalanceServlet) processRequest() method:
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException

protected void processRequest(HttpServletRequest request, HttpServletResponse


response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
double updatedBalance = (double) request.getAttribute("updatedBalance");
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet BalanceServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Transaction Successful New Balance: " + updatedBalance +
"</h1>");
out.println("</body>");
out.println("</html>");
}
}

You might also like