1 注释
<!-- --> jsp中不可见
2 <scriptlet>
<% %>: 定义局部变量,每次都初始化一次
<%! %>:定义全局变量,类及方法,不过一般用它定义全局常量(public final int a=10);<%= %>: 输出数据或内容
<%= %>输出方式与out.println()的区别
out.println()书写较复杂,且生成的代码没有缩进。
<%
int y=1;
out.println("<table border=\"1\" width=\"100%\">");
//上式需要转义,遇见“”需要注意转义。
for(int x=0;x<10;x++){
out.println("<tr>");
for(y=0;y<10;y++)
out.println("<td>"+(x*y)+"</td>");
}
out.println("</table>");
%>
<%= %>则比较简单,生成代码有缩进。
<table border="1" width="100%">
<%
for(int i=0;i<x;i++){
%>
<tr>
<%
for(int j=0;j<u;j++){
%>
<td>
<%=(i*j)%>
</td>
<%
}
%>
</tr>
<%
}
%>
</table>
下面展示一下可以用户交互的生成表单的代码:
<body>
<form action="NewFile1.jsp" method="post">
<table>
<tr>
<td>请输入要输入的行</td>
<td><input type="text" name="row"></td>
</tr>
<tr>
<td>请输入要输入的列</td>
<td><input type="text" name="col"></td>
</tr>
<tr>
<td>
<input type="submit" value="显示">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</body>
NewFile1.jsp主要代码:
<%
int x=0;
int u=0;
//捕捉异常!!!
try{
x=Integer.parseInt(request.getParameter("row"));
u=Integer.parseInt(request.getParameter("col"));
}catch(Exception e){}
%>
<table border="1" width="100%">
<%
for(int i=0;i<x;i++){
%>
<tr>
<%
for(int j=0;j<u;j++){
%>
<td>
<%=(i*j)%>
</td>
<%
}
%>
</tr>
<%
}