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

JavaServer Pages, Second Edition

[ directory ] Previous Section Next Section

2.1 Removing Text from a JSP

All it takes is a small change to Listing 2.1 in order to start exploring some of the things the JSP engine can do. Note that the HTML comment, "Our first JavaServer Page," has turned into a program instruction that sends the comment to the user.

To people building and maintaining pages, these kinds of comments are often useful because they can clarify what a block of otherwise indecipherable HTML is meant to be. However, because it is a regular part of the document, this comment will show up in the "view source" function of a user's browser.

This is typically not a problem, although it is possible for these comments to contain implementation details that might be confidential. Or maybe a page author was having a bad day and used some comment space to rant about his or her boss or relationship or the state of the world. These comments can be quite embarrassing if anyone happens to see them.

So, here is a dilemma. Comments are useful to authors but useless, or worse, for readers. JSP has a solution to this. The preceding HTML comment could be replaced with a JSP comment, like so:

<%-- Our first JavaServer Page --%>

When it sees this tag, the page compiler will recognize it as a comment and will not put it into the servlet that it builds. Hence, this comment will never be sent to the user and will not show up when the user does a view source. Again, this effect is subtle and, frankly, not that exciting. However, it does begin to show that what goes into a JSP file can and will be different from what comes out. Further, this adds a third rule to the JSP programming language: Text enclosed between comment tags (<%-- and --%>) does not turn into an instruction at all but is simply ignored.

    [ directory ] Previous Section Next Section