Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Ex NO:7

LAB-7 SERVLETS
DATE:16/11/23

AIM:
To create a servlet program to show the details of 5 students with marks of 5 subjects and
compute the GPA. Write a HTML page (form) that asks for a hobby and a name and Write
a servlet that collects the name and hobby and returns a list of other people with the same
hobby.

PROGRAM 1:
HTML
<html>
<head>
<title>Student Details</title>
</head>
<body>
<form action="result.jsp" method="post">
<label for="name">Student Name:</label>
<input type="text" id="name" name="name" required><br>
<label for="subject1">Subject 1 Marks:</label>
<input type="number" id="subject1" name="subject1" required><br>
<label for="subject2">Subject 2 Marks:</label>
<input type="number" id="subject2" name="subject2" required><br>
<label for="subject3">Subject 3 Marks:</label>
<input type="number" id="subject3" name="subject3" required><br>
<label for="subject4">Subject 4 Marks:</label>
<input type="number" id="subject4" name="subject4" required><br>
<label for="subject5">Subject 5 Marks:</label>
<input type="number" id="subject5" name="subject5" required><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
JSP
<%@ page import="java.text.DecimalFormat" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


<html>
<head>
<title>Student Result</title>
</head>
<body>
<h2>Student Result:</h2>

43 71762208043
<%
String name = request.getParameter("name");
int subject1 = parseSubject(request.getParameter("subject1"));
int subject2 = parseSubject(request.getParameter("subject2"));
int subject3 = parseSubject(request.getParameter("subject3"));
int subject4 = parseSubject(request.getParameter("subject4"));
int subject5 = parseSubject(request.getParameter("subject5"));

double totalMarks = subject1 + subject2 + subject3 + subject4 + subject5;


double gpa = totalMarks / 5.0; // Calculate average GPA

%>
<p>Name: <%= name %></p>
<p>Subject 1 Marks: <%= subject1 %></p>
<p>Subject 2 Marks: <%= subject2 %></p>
<p>Subject 3 Marks: <%= subject3 %></p>
<p>Subject 4 Marks: <%= subject4 %></p>
<p>Subject 5 Marks: <%= subject5 %></p>
<p>GPA: <%= new DecimalFormat("#.##").format(gpa) %></p>
</body>
</html>

<%!
private int parseSubject(String value) {
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
// Handle the exception, e.g., provide a default value
return 0;
}
}
%>

PROGRAM 2:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Find Friends with the Same Hobby</title>
</head>
<body>
<h2>Enter Your Name and Hobby</h2>
<form action="FindFriends.jsp" method="post">
<label for="name">Your Name:</label>
<input type="text" id="name" name="name" required><br>

44 71762208043
<label for="hobby">Your Hobby:</label>
<input type="text" id="hobby" name="hobby" required><br>
<label for="name1">Your Name:</label>
<input type="text" id="name1" name="name1" required><br>
<label for="hobby1">Your Hobby:</label>
<input type="text" id="hobby1" name="hobby1" required><br>
<input type="submit" value="Find Friends">
</form>
</body>
</html>
JSP
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.*" %>
<%
String userName = request.getParameter("name");
String userHobby = request.getParameter("hobby");
String userName1 = request.getParameter("name1");
String userHobby1 = request.getParameter("hobby1");
if (userHobby.equals(userHobby1)) {
out.println("<h2>People with the Same Hobby:</h2>");
out.println("<p>" + userName + "</p>");
out.println("<p>" + userName1 + "</p>");
} else {
out.println("<p>Hobbies are not equal. No matching friends found.</p>");
}
%>

OUTPUT:

45 71762208043
RESULT:
Thus, to create a servlet program to show the details of 5 students with marks of 5 subjects
and compute the GPA and program to find same hobby is done and verified.

46 71762208043

You might also like