JSP Programming Examples

Here, some JSP programming examples are given showing the uses of the different JSP tags. These example JSP codes are placed within MyApps directory under webapps.

 

Example 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>

 

Output:

 

 

 

 

 

 

 

 

 

 

After clicking the “Submit” button we get the following response:

 

 

Example 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>

 

Output:

 

 

 

 

 

 

 

 

 

 

After clicking the “Submit” button we get the following response:

 

 

Example 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>

 

Output:

 

 

 

 

 

 

 

 

 

 

 

Example 4

The following JSP program calculates Powers of 2 for integers in the range 0-10.

 

PowersOf2.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>

 

Output:

 

 

 

 

 

 

 

 

 

 

 

 

Example 5

The following JSP program shows a Sample Order Form.

 

OrderForm.jsp

<HTML>
<HEAD>
<TITLE>A Catalog Order Form</TITLE>
</HEAD>
<BODY>
<H1 ALIGN="center">A Sample Order Form</H1>
<%!
String item[] = {"DVD", "CD", "Diskette"};
double price[] = {19.99, 12.99, 1.99};
int quantity[] = {2, 9, 24};
%>
<TABLE ALIGN="center" BGCOLOR="lightgray" BORDER="1" WIDTH="75%">
<TR><TD>Item</TD>
<TD>Price</TD>
<TD>Quantity</TD>
<TD>Total Price</TD>
</TR>
<% for (int i=0; i<3; i++) { %>
<TR><TD><%= item[i] %></TD>
<TD><%= price[i] %></TD>
<TD><%= quantity[i] %></TD>
<TD><%= price[i] * quantity[i] %></TD>
</TR>
<% } //end for loop %>
</TABLE>
</BODY>
</HTML>

 

Output:

 

 

 

 

 

 

 

 

 

 

 

 

Example 6

The following JSP program shows a Sine Table using java.lang.Math class.

 

OrderForm.jsp

<html>
<head>
<title>Powers of 2</title>
</head>
<body>
<center>
<%!
int degrees[] = {0, 15, 30, 45, 60, 75, 90};
double number;
String result;
%>
<table border="2" align="center">
<th>Degree</th><th>Sine Value</th>
<%
for (int i=0; i<degrees.length; i++) { //start for loop
number = Math.sin(Math.toRadians(degrees[i]));
result = String.format("%.2f", number);
%>
<tr>
<td><%= degrees[i] %></td>
<td><%= result %></td>
</tr>
<% } //end for loop %>
</table>
</center>
</body>
</html>

 

Output:

 

 

 

 

 

 

 

 

 

 

Example 7

The following JSP program shows the use of JSP forward action tag.

 

ForwardAction.jsp

<html>
<head>
<title>JSP Page</title>
</head>
<body>
<% if (Math.random() > 0.5) { %>
<jsp:forward page="PowersOf2.jsp" />
<% } else { %>
<jsp:forward page="SineTable.jsp" />
<% } %>
</body>
</html>

 

Output:

Depending on the value returned by the Math.random(), the web browser either displays the contents of “PowersOf2.jsp” or displays the contents of “SineTable.jsp“. 

 

 

Example 8

The following JSP program shows the use of JSP include action tag.

 

IncludeAction.jsp

<html>
<head>
<title>JSP Page</title>
</head>
<body>
<font color="red">Include the First File:</font>
<br><br>
<jsp:include page="Date.jsp"/>
<br><br><br>
<font color="blue">Include the Second File:</font>
<br>
<jsp:include page="OrderForm.jsp"/>
</body>
</html>

 

Output: