<!--注释内容-->>
1. //
2./* */
3. <%-- 注释内容--%>
<!-- 这个注释客户端就可以看见 --> <!-- JSP中的注释,客户端无法看见 --> <% // Java中提供的单行注释,客户端无法看见 /* Java中提供的多行注释,客户端无法看见 */ %> <%-- 也看不见 --%> |
打开jsp文件后,一片空白,右键,编码选择简体中文后,右键选择查看源文件可以发现以下内容:
<!-- 这个注释客户端就可以看见 -->
<!-- JSP中的注释,客户端无法看见 -->
脚本小程序,在jsp中有3种scriptlet代码:
1. <%%>,其中可以定义局部变量,编写语句
<% int x = 10 ; // 定义局部变量 String info = "www.mldnjava.cn" ; // 局部变量 out.println("<h2>x = " + x++ + "</h2>") ; // 语句 out.println("<h2>info = " + info + "</h2>") ; // 语句 %> |
2. <%!%>,其中可以定义全局变量,方法,类
一般声明全局变量比较多,方法和类不用这种方法
<%! public static final String INFO = "www.MLDNJAVA.cn" ; int x = 10 ; %> <% out.println("<h2>x = " + x++ + "</h2>") ; // 语句 %> <%! public int add(int x,int y){ return x + y ; } %> <%! class Person{ private String name ; private int age ; public Person(String name,int age){ this.name = name ; this.age = age ; } public String toString(){ return "name = " + this.name + ";age = " + this.age ; } } %>
<% out.println("<h3>INFO = " + INFO + "</h3>") ; out.println("<h3>3 + 5 = " + add(3,5) + "</h3>") ; out.println("<h3>" + new Person("zhangsan",30) + "</h3>") ; %>
|
3. <%=%>,其中可以输出一个变量或一个具体内容
<% String info = "www.MLDNJAVA.cn" ; // 局部变量 int temp = 30 ; %> <h3>info = <%=info%></h3> <h3>temp = <%=temp%></h3> <h3>name = <%="LiXingHua"%></h3> |
问题:out.println()和 <%=%>用后者比较好,看以下的例子:
<html> <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head> <body> <% int rows = 10 ; int cols = 10 ; %> <table border="1" width="100%"> <% for (int x=0;x<rows;x++){ %> <tr> <% for(int y=0;y<cols;y++){ %> <td bgcolor="#00CC33">
<%=x*y%></td> <% } %> </tr> <% } %> </table> </body> </html>
|
虽然代码复杂,但是HTML和JAVA分离,清晰。
通过交互性打印表格:
之前打印表格的行数和列数都是固定的,下面通过一个交互性的 操作,完成用户输入表格的行数和列数 的功能,并进行显示。
input_table.html:
<html> <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head> <body> <form action="print_table.jsp" method="post"> <table border="1" width="100%"> <tr> <td>请输入要显示表格的行数:</td> <td><input type="text" name="row"></td> </tr> <tr> <td>请输入要显示表格的列数:</td> <td><input type="text" name="col"></td> </tr> <tr> <td colspan="2"> <input type="submit" value="显示"> <input type="reset" value="重置"> </td> </tr> </table> </form> </body> </html>
|
input_table.jsp
<html> <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head> <body> <% int rows = 0 ; int cols = 0 ; try{ rows = Integer.parseInt(request.getParameter("row")) ; cols = Integer.parseInt(request.getParameter("col")) ; }catch(Exception e){} %> <table border="1" width="100%"> <% for (int x=0;x<rows;x++){ %> <tr> <% for(int y=0;y<cols;y++){ %> <td bgcolor="#00CC33"><%=x*y%></td> <% } %> </tr> <% } %> </table> </body> </html>
|
JSP注释及scriptlet
原文:http://www.cnblogs.com/wujixing/p/4942345.html