jsp网页(*.jsp),就是在传统的网页html文件(*.htm/*.html)中加入java的程序片断(Scriptlet)和jsp标记(Tag)而构成的。
现在先看一个简单实例,在网页中输出一个乘法口诀表,下面为页面代码:
<%
@ page language
=
"
java
"
contentType
=
"
text/html; charset=gb2312
"
%>
< html >
< head >
< meta http - equiv = " Content-Type " content = " text/html; charset=gb2312 " >
< title > jsp 实例 </ title >
</ head >
<%-- 声明string类型str1和str2,使用嵌套循环来输出乘法口诀表 --%>
<%! String str1,str2; %>
< body bgcolor = " #ffffff " >
运行结果: < br >
<%
str1 = "" ;
for ( int i = 1 ;i < 10 ;i ++ ) ... {
for(int j=1;j<=i;j++)...{
str2=i+"×"+j+"="+i*j;
if(i!=j)...{
if((i*j)<10)...{
str1=str2+" | ";
}else...{
str1=str2+"| ";
}
}else...{
str1=str2;
}
out.print(str1);
}
out.println("<br>");
}
%>
</ body >
</ html >
< html >
< head >
< meta http - equiv = " Content-Type " content = " text/html; charset=gb2312 " >
< title > jsp 实例 </ title >
</ head >
<%-- 声明string类型str1和str2,使用嵌套循环来输出乘法口诀表 --%>
<%! String str1,str2; %>
< body bgcolor = " #ffffff " >
运行结果: < br >
<%
str1 = "" ;
for ( int i = 1 ;i < 10 ;i ++ ) ... {
for(int j=1;j<=i;j++)...{
str2=i+"×"+j+"="+i*j;
if(i!=j)...{
if((i*j)<10)...{
str1=str2+" | ";
}else...{
str1=str2+"| ";
}
}else...{
str1=str2;
}
out.print(str1);
}
out.println("<br>");
}
%>
</ body >
</ html >
运行结果:
1 × 1 = 1
2 × 1 = 2 | 2 × 2 = 4
3 × 1 = 3 | 3 × 2 = 6 | 3 × 3 = 9
4 × 1 = 4 | 4 × 2 = 8 | 4 × 3 = 12 | 4 × 4 = 16
5 × 1 = 5 | 5 × 2 = 10 | 5 × 3 = 15 | 5 × 4 = 20 | 5 × 5 = 25
6 × 1 = 6 | 6 × 2 = 12 | 6 × 3 = 18 | 6 × 4 = 24 | 6 × 5 = 30 | 6 × 6 = 36
7 × 1 = 7 | 7 × 2 = 14 | 7 × 3 = 21 | 7 × 4 = 28 | 7 × 5 = 35 | 7 × 6 = 42 | 7 × 7 = 49
8 × 1 = 8 | 8 × 2 = 16 | 8 × 3 = 24 | 8 × 4 = 32 | 8 × 5 = 40 | 8 × 6 = 48 | 8 × 7 = 56 | 8 × 8 = 64
9 × 1 = 9 | 9 × 2 = 18 | 9 × 3 = 27 | 9 × 4 = 36 | 9 × 5 = 45 | 9 × 6 = 54 | 9 × 7 = 63 | 9 × 8 = 72 | 9 × 9 = 81
1 × 1 = 1
2 × 1 = 2 | 2 × 2 = 4
3 × 1 = 3 | 3 × 2 = 6 | 3 × 3 = 9
4 × 1 = 4 | 4 × 2 = 8 | 4 × 3 = 12 | 4 × 4 = 16
5 × 1 = 5 | 5 × 2 = 10 | 5 × 3 = 15 | 5 × 4 = 20 | 5 × 5 = 25
6 × 1 = 6 | 6 × 2 = 12 | 6 × 3 = 18 | 6 × 4 = 24 | 6 × 5 = 30 | 6 × 6 = 36
7 × 1 = 7 | 7 × 2 = 14 | 7 × 3 = 21 | 7 × 4 = 28 | 7 × 5 = 35 | 7 × 6 = 42 | 7 × 7 = 49
8 × 1 = 8 | 8 × 2 = 16 | 8 × 3 = 24 | 8 × 4 = 32 | 8 × 5 = 40 | 8 × 6 = 48 | 8 × 7 = 56 | 8 × 8 = 64
9 × 1 = 9 | 9 × 2 = 18 | 9 × 3 = 27 | 9 × 4 = 36 | 9 × 5 = 45 | 9 × 6 = 54 | 9 × 7 = 63 | 9 × 8 = 72 | 9 × 9 = 81
这个实例首先是jsp指令,<%@ page language="java" contentType="text/html; charset=gb2312"%>,设置了该页面使用的脚本语言和编码。
其次是jsp声明,<%! String str1,str2; %>,申明了两个String类型的变量;
最后是jsp页面处理逻辑的java代码块,置于<% %>之间,在程序代码块中使用了内置对象“out”,ou't.print()方法可以讲字符串发送到客户的浏览器。
事实上,java和html标记的结合是完美的,可以将一个程序代码块用多个<% %>来分成几个部分,而浏览器会自己识别程序和html标记的,所以上面的程序也可以写成下面的形式:
<%
@ page language
=
"
java
"
contentType
=
"
text/html; charset=gb2312
"
%>
< html >
< head >
< meta http - equiv = " Content-Type " content = " text/html; charset=gb2312 " >
< title > jsp 实例 </ title >
</ head >
<%-- 声明string类型str1和str2,使用嵌套循环来输出乘法口诀表 --%>
<%! String str1,str2; %>
< body bgcolor = " #ffffff " >
运行结果: < br >
<%
str1 = "" ;
for ( int i = 1 ;i < 10 ;i ++ ) ... {
for(int j=1;j<=i;j++)...{
str2=i+"×"+j+"="+i*j;
if(i!=j)...{
if((i*j)<10)...{
str1=str2+" | ";
}else...{
str1=str2+"| ";
}
}else...{
str1=str2;
}
%>
<%=str1 %>
<% } %>
<br>
<% } %>
</ body >
</ html >
< html >
< head >
< meta http - equiv = " Content-Type " content = " text/html; charset=gb2312 " >
< title > jsp 实例 </ title >
</ head >
<%-- 声明string类型str1和str2,使用嵌套循环来输出乘法口诀表 --%>
<%! String str1,str2; %>
< body bgcolor = " #ffffff " >
运行结果: < br >
<%
str1 = "" ;
for ( int i = 1 ;i < 10 ;i ++ ) ... {
for(int j=1;j<=i;j++)...{
str2=i+"×"+j+"="+i*j;
if(i!=j)...{
if((i*j)<10)...{
str1=str2+" | ";
}else...{
str1=str2+"| ";
}
}else...{
str1=str2;
}
%>
<%=str1 %>
<% } %>
<br>
<% } %>
</ body >
</ html >
这个程序和上面的运行结果是一致的。