Scriptlet的三种形式:
Ps: <% %>在注释中必须转义, <%=%>不能嵌套在 <% %>中
<html>
<!-- HTML注释(客户端可见) -->
<%-- JSP注释(客户端不可见) --%>
<%-- 一:Scriptlet的<%!%>用法 --%>
<%!
/*1:是定义全局变量和常量
x++后会变化,如果定义在/</%/%/>中,刷新则无变化*/
int x = 0; //静态
%>
<%!
/*2:是定义方法和类
如果放在/</%/%/>中,则编译会出错*/
public int add(int x,int y)
{
return (x+y);
}
%>
<%!
public class Person
{
private String name = "rt";
public String toString()
{
return this.name;
}
}
%>
<%-- 二:用Scriptlet的 /</%/=/%/> 语法,直接输出 --%>
<h3>自增前 x=> <%=x%> </h3>
<%
out.println("<h3>new Person()=>"+ new Person() +"</h3>");
out.println("<h3>add(1,2)=>"+ add(1,2) +"</h3>");
out.println("<h3>x++=>"+ (++x) +"</h3>");
%>
<!-- /</%/%/>在注释中必须转义, /</%/=/%/>不能嵌套在 /</% /%/>中-->
</html>
html输入数据,提交到jsp做显示
<html>
<head>
<title> 标题 </title>
</head>
<body>
<form action="print_table.jsp" method="post">
<table border="1" width="100%">
<tr>
<td>输入要显示的行数</td>
<td><input type="text" name="rows"></td>
</tr>
<tr>
<td>输入要显示的列数</td>
<td><input type="text" name="cols"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交至JSP">
<input type="reset" value="重置">
<td>
<tr>
</table>
</form>
</body>
</html>
<html>
<body>
<%
int rows = 0;
int cols = 0;
try{
rows = Integer.parseInt(request.getParameter("rows"));
cols = Integer.parseInt(request.getParameter("cols"));
}catch (Exception e)
{
out.println("<h2>输入有误</h2>");
}
%>
<table border="1" width="70%">
<%
for(int i=1;i<=rows;i++)
{
%>
<tr>
<%
for(int j=1;j<=cols;j++)
{
%>
<td> <%=(i*j)%> </td>
<%
}
%>
</tr>
<%
}
%>
</table>
</body>