Java Practical Assignment
Assignment Questions
[Link] Question
1 Create your profile page like education, details, hobbies etc.
Create a registration form using different components and print all the values
2
using JSP.
3 Design a calculator using JSP.
A web application that takes name and age from an HTML page. If age < 18,
4 show: Hello <name>, you are not authorized to visit the page. Else: Welcome
<name>, to this site.
Solutions
Q1. Profile Page – [Link]
jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My Profile</title>
<style>
body { font-family: Arial; background: #f4f4f9; padding: 20px; }
.container { max-width: 600px; margin: auto; background: white; padding: 20px;
border-radius: 10px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }
h1 { color: #2c3e50; text-align: center; }
p { font-size: 16px; line-height: 1.6; }
pg. 1
ul { padding-left: 20px; }
</style>
</head>
<body>
<div class="container">
<h1>Student Profile</h1>
<p><strong>Name:</strong> Aryan Sharma</p>
<p><strong>Roll No:</strong> CS2021001</p>
<p><strong>Branch:</strong> [Link] Computer Science & Engineering</p>
<p><strong>Semester:</strong> 5th</p>
<p><strong>College:</strong> XYZ Institute of Technology</p>
<h3>Education</h3>
<ul>
<li>10th: 95% (CBSE, 2018)</li>
<li>12th: 92% (Science Stream, 2020)</li>
<li>[Link] CSE: Pursuing (CGPA: 9.1)</li>
</ul>
<h3>Hobbies</h3>
<ul>
<li>Coding & Problem Solving</li>
<li>Cricket & Gym</li>
<li>Watching Tech Reviews</li>
<li>Listening to Music</li>
</ul>
<p style="text-align:center; margin-top:30px;">
pg. 2
<a href="[Link]">Back to Home</a>
</p>
</div>
</body>
</html>
Q2. Registration Form + JSP Output
[Link]
html
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
<style>
body { font-family: Arial; background: #f0f2f5; padding: 30px; }
form { max-width: 400px; margin: auto; background: white; padding: 20px; border-
radius: 8px; }
input, select, button { width: 100%; padding: 10px; margin: 8px 0; border: 1px solid
#ccc; border-radius: 4px; }
button { background: #3498db; color: white; cursor: pointer; }
button:hover { background: #2980b9; }
</style>
</head>
<body>
<form action="[Link]" method="post">
<h2 style="text-align:center;">Registration Form</h2>
<input type="text" name="name" placeholder="Full Name" required><br>
<input type="email" name="email" placeholder="Email" required><br>
<input type="password" name="pass" placeholder="Password" required><br>
pg. 3
<select name="gender" required>
<option value="">Select Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select><br>
<input type="checkbox" name="hobbies" value="Coding"> Coding
<input type="checkbox" name="hobbies" value="Gaming"> Gaming
<input type="checkbox" name="hobbies" value="Reading"> Reading<br><br>
<button type="submit">Register</button>
</form>
</body>
</html>
[Link]
jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Registration Details</title>
<style>
body { font-family: Arial; background: #e8f5e9; padding: 30px; }
.box { max-width: 500px; margin: auto; background: white; padding: 20px; border-
radius: 8px; }
li { margin: 5px 0; }
</style>
</head>
<body>
pg. 4
<div class="box">
<h2>Registration Successful!</h2>
<%
[Link]("UTF-8");
String name = [Link]("name");
String email = [Link]("email");
String pass = [Link]("pass");
String gender = [Link]("gender");
String[] hobbies = [Link]("hobbies");
%>
<ul>
<li><strong>Name:</strong> <%= name %></li>
<li><strong>Email:</strong> <%= email %></li>
<li><strong>Password:</strong> <%= [Link](".", "*") %> (hidden)</li>
<li><strong>Gender:</strong> <%= gender %></li>
<li><strong>Hobbies:</strong>
<%= (hobbies != null) ? [Link](", ", hobbies) : "None selected" %>
</li>
</ul>
<p style="text-align:center;"><a href="[Link]">Register Another</a></p>
</div>
</body>
</html>
Q3. Calculator – [Link]
jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
pg. 5
<!DOCTYPE html>
<html>
<head>
<title>JSP Calculator</title>
<style>
body { font-family: Arial; background: #fff3e0; padding: 30px; text-align: center; }
form { display: inline-block; background: white; padding: 20px; border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1); }
input[type="number"], select, input[type="submit"] { padding: 10px; margin: 5px;
font-size: 16px; }
input[type="submit"] { background: #e67e22; color: white; border: none; cursor:
pointer; }
input[type="submit"]:hover { background: #d35400; }
.result { margin-top: 20px; font-size: 20px; color: #27ae60; }
</style>
</head>
<body>
<h1>Simple Calculator</h1>
<form method="post">
<input type="number" name="num1" placeholder="Number 1" required step="any">
<select name="op" required>
<option value="+">+</option>
<option value="-">-</option>
<option value="*">×</option>
<option value="/">÷</option>
y>
</select>
<input type="number" name="num2" placeholder="Number 2" required step="any">
<br><br>
pg. 6
<input type="submit" value="Calculate">
</form>
<%
String n1 = [Link]("num1");
String n2 = [Link]("num2");
String op = [Link]("op");
if (n1 != null && n2 != null && op != null) {
try {
double a = [Link](n1);
double b = [Link](n2);
double result = 0;
String expr = "";
switch (op) {
case "+": result = a + b; expr = a + " + " + b; break;
case "-": result = a - b; expr = a + " - " + b; break;
case "*": result = a * b; expr = a + " × " + b; break;
case "/":
if (b == 0) { [Link]("<div class='result'>Error: Division by zero!</div>"); return;
}
result = a / b; expr = a + " ÷ " + b; break;
[Link]("<div class='result'>" + expr + " = " + result + "</div>");
} catch (Exception e) {
[Link]("<div class='result' style='color:red;'>Invalid input!</div>");
%>
pg. 7
</body>
</html>
Q4. Age-Based Access Control
[Link]
html
<!DOCTYPE html>
<html>
<head>
<title>Enter Details</title>
<style>
body { font-family: Arial; background: #dfe6e9; padding: 50px; text-align: center; }
form { display: inline-block; background: white; padding: 30px; border-radius: 10px;
}
input { padding: 10px; margin: 10px; width: 200px; }
button { padding: 10px 20px; background: #00b894; color: white; border: none;
cursor: pointer; }
button:hover { background: #00a07a; }
</style>
</head>
<body>
<h1>Site Access</h1>
<form action="[Link]" method="post">
<input type="text" name="name" placeholder="Your Name" required><br>
<input type="number" name="age" placeholder="Your Age" required
min="1"><br><br>
<button type="submit">Enter Site</button>
</form>
</body>
pg. 8
</html>
[Link]
jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Access Result</title>
<style>
body { font-family: Arial; background: #fd79a8; color: white; text-align: center;
padding: 50px; }
.msg { font-size: 28px; margin-top: 50px; }
a { color: #fff; text-decoration: underline; }
</style>
</head>
<body>
<%
String name = [Link]("name");
String ageStr = [Link]("age");
int age = 0;
try {
age = [Link](ageStr);
} catch (Exception e) {
[Link]("<div class='msg'>Invalid age!</div>");
return;
if (age < 18) {
pg. 9
%>
<div class="msg">Hello <%= name %>, you are not authorized to visit the page</div>
<%
} else {
%>
<div class="msg" style="background:#00b894; padding:20px; border-radius:10px;
display:inline-block;">
Welcome <%= name %>, to this site
</div>
<%
%>
<br><br>
<a href="[Link]">Go Back</a>
</body>
</html>
pg. 10