0% found this document useful (0 votes)
29 views

JSP Programs

The document contains several JSP programs that demonstrate different functionalities. It includes programs for calculating factorial values, generating Fibonacci series, displaying system date and time, and calculating powers of 2. Each program takes user input from an HTML form and displays the output accordingly.

Uploaded by

waben61343
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

JSP Programs

The document contains several JSP programs that demonstrate different functionalities. It includes programs for calculating factorial values, generating Fibonacci series, displaying system date and time, and calculating powers of 2. Each program takes user input from an HTML form and displays the output accordingly.

Uploaded by

waben61343
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

JSP Programs

1. The following JSP program calculates factorial values for an integer number, while
the input is taken from an HTML form.

Input.html
<html>
<body>
<form action="Factorial.jsp">
Enter a value for n: <input type="text" name="val">
<input type="submit" value="Submit">
</form>
</body>
</html>

Factorial.jsp
<html>
<body>
<%!
long n, result;
String str;

long fact(long n) {
if(n==0)
return 1;
else
return n*fact(n-1);
}
%>
<%
str = request.getParameter("val");
n = Long.parseLong(str);
result = fact(n);
%>
<b>Factorial value: </b> <%= result %>
</body>
</html>
2. The following JSP program shows the Fibonacci series up to a particular term, while the
input is taken from an HTML form.

Input.html

<html>
<body>
<form action="Fibonacci.jsp">
Enter a value for n: <input type="text" name="val">
<input type="submit" value="Submit">
</form>
</body>
</html>

Fibonacci.jsp
<html>
<body>
<%!
int n;
String str;

int fibo(int n) {
if(n<2)
return n;
else
return fibo(n-1) + fibo(n-2);
}
%>
<b>Fibonacci series: </b><br>
<%
str = request.getParameter("val");
n = Integer.parseInt(str);

for(int i=0; i<=n; i++) {


out.print(fibo(i) + " ");
}
%>
</body>
</html>
3. The following JSP program shows the System date and time.

Date.jsp

<html>
<body>
<%-- JSP comments --%>
<%@page import="java.util.Date"%>
<%!
Date date;
%>
<%
date = new Date();
%>
<b>System date and time: </b> <%= date %>
</body>
</html>

4. The following JSP program calculates Powers of 2 for integers


in the range 0-10.

Powerof2.jsp

<html>
<head>
<title>Powers of 2</title>
</head>
<body>
<center>
<table border="2" align="center">
<th>Exponent</th>
<th>2^Exponent</th>
<% for (int i=0; i<=10; i++) { //start for loop %>
<tr>
<td><%= i%></td>
<td><%= Math.pow(2, i) %></td>
</tr>
<% } //end for loop %>
</table>
</center>
</body>
</html>

You might also like