JSP中使用四种注释的方式:
1 <%-- comments --%>
2 // comments
3 /** comments **/
4 <!-- comments -->
JSP本身包括两种不同的注释:
一种是<!-- comments -->,这种HTML风格的注释完全被JSP引擎和浏览器忽视,不做任何处理。本质上是被当作template data,可以在客户端通过浏览器的查看源代码显示。使用这种注释的目的是为了良好的编码风格,增强可读性。
另外一种是<%-- comments --%>,这种注释的内容会被JSP引擎解析处理,并且在生成新的页面前被"毁尸灭迹"。本质上是被当作element data,不可以在客户端被显示。通常使用这种注释是不想在客户端暴露实现的逻辑,增强安全性。
// comments 和 /** comments **/ 是java编码风格的注释,用于脚本元素中。例如:
<%
// 得到系统当前的calendar
java.util.Calendar currentCal = java.util.Calendar.getInstance();
/**
计算年月日,生成日历表
**/
// Set the current day of the month
int currentDay =
currentCal.get(currentCal.DAY_OF_MONTH);
// Calculate the totals days in the month
int daysInMonth =
currentCal.getActualMaximum(currentCal.DAY_OF_MONTH);
// Calculate the day of the week for the first
currentCal.set(currentCal.DAY_OF_MONTH, 1);
int dayOfWeek = currentCal.get(currentCal.DAY_OF_WEEK);
// Prefill the calendar with blank spaces
if (dayOfWeek != 1) {
out.println(" <TD COLSPAN=" + (dayOfWeek-1) +
"> </TD>");
}
// Fill in dates
for (int day=1; day <= daysInMonth; day++) {
if (day == currentDay) {
out.println(" <TD CLASS='currentDay'>" + day +
"</TD>");
} else {
out.println(" <TD CLASS='otherDay'>" + day +
"</TD>");
}
if (dayOfWeek == 7) {
out.println(" </TR>/n/n <TR>");
dayOfWeek = 1;
} else {
dayOfWeek++;
}
}
// Postfill the calendar with blank spaces
if ((8-dayOfWeek) != 0) {
out.println(" <TD COLSPAN=" + (8-dayOfWeek) +
"> </TD>");
}
%>