I. Indroduction
JSP stands for JavaServer Pages. Like JavaScript,
or PHP languages, where their scripts reside within HTML tags;
JSP uses exactly the same manner.
The JSP program saved as file.jsp contains Java code
between the scriptlet <% %>; wrapped in HTML tags using
some features, mainly: declarations, expressions,
and directives like:
<%! declarations %> as
private int Fibonacci(int n){
if(n <= 1){return n;}
else{return (Fibonacci(n - 1) + Fibonacci(n - 2));}}
<%= expression %> as
<%= (new java.util.Date() ).toLocaleString() %>
<%@ directive attribute1="value1" %> as
<%@ page import="java.util.*" %>
<%@ page import = "package1.*, package2,*,..." %>
JSP and Servlets have the same purpose; To run Java
programs wrapped in HTML tags in the server side.
Once Tomcat is installed and configured, set the file.jsp as the following:
1. The location of the file.jsp is:
C:\Program Files\Apache Software Foundation\Tomcat 6.0\
webapps\examples\
where a subdirectory is created such as NewWork\. Finally,
files.jsp are nested in:
C:\Program Files\Apache Software Foundation\Tomcat 6.0\
webapps\examples\NewWork\
2. To execute this program file.jsp, call it at:
http://localhost/examples/NewWork/file.jsp
(The related port for Tomcat,here, is set at localhost)
2. Examples:
Example1
<html>
<head>
<title>JSP Test Page: Regards</title>
</head>
<body>
<h1>Regards</h1>
<%
out.println("\t<P> To say: Regards </P>");
for(int c = 1; c < 3; c++)
{
out.println("\t<P> " + c +". Regards </P>");
if (c==2){ out.println("\t<P> " + c +". All the best
of luck. </P>");}
}
%>
<BR>
<%
out.println("\t<P> To make the factorial of the three first
numbers </P>");
int k = 1;
for(int j = 0; j <= 7; j++)
{
if (j <=1){
out.println("\t<P> the factorial of " + j + "
is 0. </P>");
}
else {
k = k*j;
out.println("\t<P> The factorial of " + j + " is
" + k + " </P>");
}
}
%>
</body>
</html>
Example 2:
<html>
<head>
<title>Some Math</title>
<%!
private static double PI = 3.1415;
private int Fibonacci(int n)
{
if(n <= 1){
return n;
}
else{
return (Fibonacci(n - 1) + Fibonacci(n - 2));
}
}
%>
</head>
<body>
<h2>JSP Fibonacci program</h2>
<p>The Fobonacci of 7 is : <%= Fibonacci(7) %></p>
<p> PI * Fobonacci of 7 is : <%= PI * Fibonacci(7) %></p>
<p><font size = "<%=4%>">Right now, the time is :
</font></p>
<%@ page import="java.util.*" %>
<%= (new java.util.Date() ).toLocaleString() %>
</body>
</html>
|