Slip No 9 Q B

Write a JSP program to create an online shopping mall. User must be allowed to do purchase from two pages. Each page should have a page total. The third page should display a bill, which consists of a page total of whatever the purchase has been done and print the total. (Use Session)

Page 1 – Purchase Page (Page1.jsp)

<%@ page language="java" %>
<html>
<head>
    <title>Shopping Mall - Page 1</title>
</head>
<body>

<h2>Online Shopping Mall - Page 1</h2>

<form action="Page2.jsp" method="post">
    Item 1 (₹100):
    <input type="number" name="item1" value="0"><br><br>

    Item 2 (₹200):
    <input type="number" name="item2" value="0"><br><br>

    <input type="submit" value="Go to Page 2">
</form>

<%
    int item1 = 0, item2 = 0;

    if (request.getParameter("item1") != null) {
        item1 = Integer.parseInt(request.getParameter("item1"));
        item2 = Integer.parseInt(request.getParameter("item2"));
    }

    int page1Total = (item1 * 100) + (item2 * 200);

    session.setAttribute("page1Total", page1Total);
%>

<p><b>Page 1 Total:</b> ₹<%= page1Total %></p>

</body>
</html>

Page 2 – Purchase Page (Page2.jsp)

<%@ page language="java" %>
<html>
<head>
    <title>Shopping Mall - Page 2</title>
</head>
<body>

<h2>Online Shopping Mall - Page 2</h2>

<form action="Bill.jsp" method="post">
    Item 3 (₹150):
    <input type="number" name="item3" value="0"><br><br>

    Item 4 (₹250):
    <input type="number" name="item4" value="0"><br><br>

    <input type="submit" value="Generate Bill">
</form>

<%
    int item3 = 0, item4 = 0;

    if (request.getParameter("item3") != null) {
        item3 = Integer.parseInt(request.getParameter("item3"));
        item4 = Integer.parseInt(request.getParameter("item4"));
    }

    int page2Total = (item3 * 150) + (item4 * 250);

    session.setAttribute("page2Total", page2Total);
%>

<p><b>Page 2 Total:</b> ₹<%= page2Total %></p>

</body>
</html>

Bill Page – Final Bill (Bill.jsp)

<%@ page language="java" %>
<html>
<head>
    <title>Final Bill</title>
</head>
<body>

<h2>Final Bill</h2>

<%
    int page1Total = (Integer) session.getAttribute("page1Total");
    int page2Total = (Integer) session.getAttribute("page2Total");

    int grandTotal = page1Total + page2Total;
%>

<table border="1" cellpadding="10">
    <tr>
        <th>Description</th>
        <th>Amount (₹)</th>
    </tr>
    <tr>
        <td>Page 1 Total</td>
        <td><%= page1Total %></td>
    </tr>
    <tr>
        <td>Page 2 Total</td>
        <td><%= page2Total %></td>
    </tr>
    <tr>
        <th>Grand Total</th>
        <th><%= grandTotal %></th>
    </tr>
</table>

</body>
</html>
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top