站内搜索: 请输入搜索关键词
当前页面: 图书首页 > JavaServer Pages, Second Edition

JavaServer Pages, Second Edition

[ directory ] Previous Section Next Section

2.2 JSP Errors

As smart as the JSP engine is, it is also very literal minded. Like any other program ever written, the best it can manage is to do what we say, which is not always the same thing as to do what we want. When a JSP page does not specify what to do in exactly the right way, the JSP engine sometimes has no alternative but to give up, return an error page, and ask for help.

One common error is leaving out a closing tag. This might happen if a page author tries to close a JSP comment as if it were an HTML comment, as in <%-- our first JSP -->.

A user who tries to access this page will receive a rather unsettling page giving a great deal of information about the cause and nature of the error. The exact format of this message varies between JSP engines; Tomcat generates a page such as the one shown in Figure 2.1.

Figure 2.1. The Tomcat error page.

graphics/02fig01.jpg

The most significant part of this message is the first exception line, which contains the following:

/error1.jsp(1.4) Unterminated <%-- tag

This line concisely specifies what the problem is, along with the name of the file in which the error occurred. The numbers in parentheses are the line number and the number of characters within the line where the problematic tag starts.

A variation of this problem is even more insidious. Consider the following JSP snippet:

<%-- This is a comment -->
Hello, world.
<%-- This is another comment --%>

When a browser requests this page, the content will be missing, but no error will be generated. The reason is that this time, the comment tag is closed; it just happens to be closed by the second comment tag! This means that the page compiler will consider "Hello, world" as part of the comment and will discard it.

    [ directory ] Previous Section Next Section