HTML注释
在客户端显示一个注释。
JSP 语法
<!-- comment [ <%= expression %> ] -->
<!-- This file displays the user login screen --> 在客户端的HTML源代码中产生和上面一样的 数据: <!-- This file displays the user login screen -->
<!-- This page was loaded on <%= (new java.util.Date()).toLocaleString() %> -->
<!-- This page was loaded on January 1, 2000 -->
<%-- comment --%>
<%@ page language="java" %> <html> <head><title>A Comment Test</title></head> <bo dy> <h2>A Test of Comments</h2> <%-- This comment will not be visible in the page sour ce --%> </body> </html>
<%! declaration; [ declaration; ]+ ... %>
<%! int i = 0; %> <%! int a, b, c; %> <%! Circle a = new Circle(2.0); %>
<%= expression %>
<font color="blue"><%= map.size() %></font> <b><%= numguess.getHint() %></b>.
描述
表达式元素表示的是一个在脚本语言中被定义的表达式,在运行后被自动转化为字符串,然后插入到这个表达式在JSP文件的位置显示。因为这个表达式的值已经被转化为字符串,所以你能在一行文本中插入这个表达式(形式和ASP完全一样)。
当你在JSP中使用表达式时请记住以下几点:
1 你不能用一个分号(“;”)来作为表达式的结束符。但是同样的表达式用在scriptlet中就需要以分号来结尾了!查看Scriptlet这个表达式元素能够包括任何在Java Language Specification中有效的表达式。
2 有时候表达式也能作为其它JSP元素的属性值。一个表达式能够变得很复杂,它可能由一个或多个表达式组成,这些表达式的顺序是从左到右。
Scriptlet
包含一个有效的程序段。
JSP 语法
<% code fragment %>
例子
<% String name = null; if (request.getParameter("name") == null) { %> <%@ include file="error.html" %> <% } el se { foo.setName(request.getParameter("name")); if (foo.getName().equalsIgnoreCase("integra")) na me = "acura"; if (name.equalsIgnoreCase( "acura" )) { %>
描述
一个scriptlet能够包含多个jsp语句、方法、变量、表达式。
因为有了scriptlet,我们便能做以下的事:
1 声明将要用到的变量或方法(参考 声明)。
2 编写JSP表达式(参考 表达式)。
3 使用任何隐含的对象和任何用jsp:useBean声明过的对象。
4 编写JSP语句(如果你在使用Java语言,这些语句必须遵从Java Language Specification)。
任何文本、HTML标记、JSP元素必须在scriptlet之外。
当JSP收到客户的请求时,scriptlet就会被执行,如果scriptlet有显示的内容,这些显示的内容就被存在out对象中。
Page 指令
定义JSP文件中的全局属性。
JSP 语法
<%@ page [ language="java" ] [ extends="package.class" ] [ import="{package.class | packa ge.*}, ..." ] [ session="true | false" ] [ buffer="none | 8kb | sizekb" ] [ autoFlush="true | false" ] [ isThre adSafe="true | false" ] [ info="text" ] [ errorPage="relativeURL" ] [ contentType="mimeType [ ;chars et=characterSet ]" | "text/html; charset=ISO-8859-1" ] [ isErrorPage="true | false" ] %>
<%@ page import="java.util.*, java.lang.*" %> <%@ page buffer="5kb" autoFlush="false" %> <%@ pa ge errorPage="error.jsp" %>
描述
%@ page %指令作用于整个JSP页面,同样包括静态的包含文件。但是% @ page %指令不能作用于动态的包含文件,比如jsp:include。
你可以在一个页面中用上多个% @ page %指令,但是其中的属性只能用一次,不过也有个例外,那就是import属性。因为import属性和Java中的import语句差不多(参照Java Language),所以你就能多用此属性几次了。
无论你把% @ page %指令放在JSP的文件的哪个地方,它的作用范围都是整个JSP页面。不过,为了JSP程序的可读性,以及好的编程习惯,最好还是把它放在JSP文件的顶部。
Taglib 指令
定义一个标签库以及