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

Spring 2024 - CS311 - 2

Uploaded by

zara mahmood
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)
29 views3 pages

Spring 2024 - CS311 - 2

Uploaded by

zara mahmood
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/ 3

Assignment No.

02
Total Marks: 20
Semester: Spring 2024
Topics Covered: 190-210
Introduction to Web Services Development – CS311 Due Date: 24th June, 2024

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

 The assignment is submitted after due date.


 The assignment is submitted via email.
 The assignment is copied from Internet or from any other student.
 The submitted assignment does not open or file is corrupt.

Objectives:
To understand and get hands on experience of:

 HTML

 Servlet

Note: All types of plagiarism are strictly prohibited.

For any query about the assignment, contact at [email protected]

Assignment Submission Instructions

Word format (.doc or docx) file should be upload.


Problem Statement: Marks 20

Consider the following two files (MyForm.html and MyResults.java). You are required to write
missing code and correct the code in case of error (if any) for MyForm.html which reads any number
to calculate the square and factorial and sends them to MyResults.java Servlet. NowMyResults.java
Servlet will calculate result for the square and factorial of that numbers and display output.

Write the missing code of MyForm.html and MyResults.java (at points mentioned in code below
with … and in red color comments). Exception handling should be done as well wherever
necessary.

MyForm.html
<form action="MyResults" method="post"> // write code here for method type

<label for="num">Enter Number:</label>


<input type="number" id="num" name="num"/><br/> // Write type
<input type="submit" value="Result"/>
</form > // write code here to close tag

<!DOCTYPE html>
<html>
<head>
<title>Calculate Square and Factorial</title>
<meta charset=”UTF-8”>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0”>
</head>
<body>
<form action="MyResults" method="post">
<label for="num">Enter Number:</label>
<input type="number" id="num" name="num"/><br/>
<input type="submit" value="Result"/>
</form>
</body>
</html>

MyResults.java

// write code here to import required packages

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyResult extends HttpServlet {


public void doPost(HttpServletRequest req,HttpServletResponse res)

throws IOException , ServletException {

// write code here to get value of number to calculate square and factorial and display result

String numberStr = req.getParameter("num");

// Handle potential NumberFormatException

int number;
try {
number = Integer.parseInt(numberStr);
}
catch (NumberFormatException e) {

// Handle the case where the user entered a non-numeric value

res.getWriter().println("Invalid input. Please enter a number.");


return;
}

// Calculate square and factorial

int square = number * number;


int factorial = 1;
for (int i = 1; i <= number; i++) {
factorial *= i;
}

// Prepare the response message

String message = "Number: " + number + "<br>";


message += "Square: " + square + "<br>";
message += "Factorial: " + factorial + "<br>";

// Write the response to the client

res.setContentType("text/html");
res.getWriter().println(message);
}
}

NOTE: Submit in Word format (.doc or docx)

BEST OF LUCK

You might also like