Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
32
Chapter 2 First Servlets
HTML 3.2 and 4.0 specifications require it. I strongly discourage this prac
tice. The advantage of the
DOCTYPE
line is that it tells HTML validators
which version of HTML you are using, so they know which specification to
check your document against. These validators are very valuable debugging
services, helping you catch HTML syntax errors that your browser guesses
well on, but that other browsers will have trouble displaying. The two most
popular on line validators are the ones from the World Wide Web Consor
tium (
http://validator.w3.org/
) and from the Web Design Group
(
http://www.htmlhelp.com/tools/validator/
). They let you submit a
URL, then they retrieve the page, check the syntax against the formal HTML
specification, and report any errors to you. Since a servlet that generates
HTML looks like a regular Web page to visitors, it can be validated in the
normal manner unless it requires
POST
data to return its result. Remember
that
GET
data is attached to the URL, so you can submit a URL that includes
GET
data to the validators.
Core Approach
Use an HTML validator to check the syntax of pages that your servlets
generate.
Admittedly it is a bit cumbersome to generate HTML with
println
state
ments, especially long tedious lines like the
DOCTYPE
declaration. Some people
address this problem by writing detailed HTML generation utilities in Java,
then use them throughout their servlets. I'm skeptical of the utility of an exten
sive library for this. First and foremost, the inconvenience of generating
HTML programmatically is one of the main problems addressed by JavaServer
Pages (discussed in the second part of this book). JSP is a better solution, so
don't waste effort building a complex HTML generation package. Second,
HTML generation routines can be cumbersome and tend not to support the
full range of HTML attributes (
CLASS
and
ID
for style sheets, JavaScript event
handlers, table cell background colors, and so forth). Despite the questionable
value of a full blown HTML generation library, if you find you're repeating the
same constructs many times, you might as well create a simple utility file that
simplifies those constructs. For standard servlets, there are two parts of the
Web page (
DOCTYPE
and
HEAD
) that are unlikely to change and thus could ben
efit from being incorporated into a simple utility file. These are shown in List
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved