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

JSP Programs

The document contains six JSP programs demonstrating basic functionalities such as displaying 'Hello World', calculating the sum of two numbers, printing even numbers, finding the factorial of a number, generating a Fibonacci series, and displaying the weekday based on user input. Each program includes HTML forms for user input and Java code for processing the input and displaying results. These examples serve as foundational exercises for learning JSP programming.

Uploaded by

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

JSP Programs

The document contains six JSP programs demonstrating basic functionalities such as displaying 'Hello World', calculating the sum of two numbers, printing even numbers, finding the factorial of a number, generating a Fibonacci series, and displaying the weekday based on user input. Each program includes HTML forms for user input and Java code for processing the input and displaying results. These examples serve as foundational exercises for learning JSP programming.

Uploaded by

bishramoraon896
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JSP Programs - First 6 Tasks

1. JSP Program to Display 'Hello World'


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>Hello World</h2>
</body>
</html>

2. JSP Program to Find the Sum of Two Numbers

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


<!DOCTYPE html>
<html>
<head>
<title>Sum of Two Numbers</title>
</head>
<body>
<form method="post">
Enter first number: <input type="text" name="num1"><br>
Enter second number: <input type="text" name="num2"><br>
<input type="submit" value="Calculate">
</form>

<%
String n1 = request.getParameter("num1");
String n2 = request.getParameter("num2");

if (n1 != null && n2 != null && !n1.isEmpty() && !n2.isEmpty()) {


int sum = Integer.parseInt(n1) + Integer.parseInt(n2);
%>
<h3>Sum: <%= sum %></h3>
<%
}
%>
</body>
</html>
3. JSP Program to Print Even Numbers

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


<!DOCTYPE html>
<html>
<head>
<title>Even Numbers</title>
</head>
<body>
<h2>Even Numbers from 1 to 20:</h2>
<ul>
<%
for (int i = 2; i <= 20; i += 2) {
%>
<li><%= i %></li>
<%
}
%>
</ul>
</body>
</html>

4. JSP Program to Find Factorial of a Given Number

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


<!DOCTYPE html>
<html>
<head>
<title>Factorial</title>
</head>
<body>
<form method="post">
Enter a number: <input type="text" name="num"><br>
<input type="submit" value="Calculate Factorial">
</form>

<%
String numStr = request.getParameter("num");
if (numStr != null && !numStr.isEmpty()) {
int num = Integer.parseInt(numStr);
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
%>
<h3>Factorial of <%= num %> is <%= fact %></h3>
<%
}
%>
</body>
</html>

5. JSP Program to Generate Fibonacci Series

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


<!DOCTYPE html>
<html>
<head>
<title>Fibonacci Series</title>
</head>
<body>
<form method="post">
Enter number of terms: <input type="text" name="terms"><br>
<input type="submit" value="Generate Fibonacci">
</form>

<%
String termsStr = request.getParameter("terms");
if (termsStr != null && !termsStr.isEmpty()) {
int terms = Integer.parseInt(termsStr);
int a = 0, b = 1, c;
%>
<h3>Fibonacci Series:</h3>
<p><%= a %>, <%= b %>
<%
for (int i = 2; i < terms; i++) {
c = a + b;
a = b;
b = c;
%>
, <%= c %>
<%
}
%>
</p>
<%
}
%>
</body>
</html>
6. JSP Program to Display Weekday Based on Input (0-6)

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


<!DOCTYPE html>
<html>
<head>
<title>Weekday Finder</title>
</head>
<body>
<form method="post">
Enter a number (0-6): <input type="text" name="day"><br>
<input type="submit" value="Find Weekday">
</form>

<%
String dayStr = request.getParameter("day");
if (dayStr != null && !dayStr.isEmpty()) {
int day = Integer.parseInt(dayStr);
String weekday;
switch (day) {
case 0: weekday = "Sunday"; break;
case 1: weekday = "Monday"; break;
case 2: weekday = "Tuesday"; break;
case 3: weekday = "Wednesday"; break;
case 4: weekday = "Thursday"; break;
case 5: weekday = "Friday"; break;
case 6: weekday = "Saturday"; break;
default: weekday = "Invalid Input! Please enter a number between 0 and 6."; break;
}
%>
<h3>Weekday: <%= weekday %></h3>
<%
}
%>
</body>
</html>

You might also like