Server-Side Web Programming
Server-Side Web Programming
Web Programming
Lecture 6:
Java Servlets and the
web.xml Configuration File
Configuration Files
• Each webapp has a
web.xml file
– In WEB-INF subdirectory
• Defines:
– Mapping from servlet names
to servlet files
– Global constants
– Default error pages
– Security roles…
The web.xml file
• Complex (and finicky) XML
• If buggy, application will not load
– Errors displayed in Tomcat window when started
• NetBeans provides interface to manipulate its values
The web.xml file
• Internally, still XML
– Can view and edit at
XML tab
– Basic form of XML:
<tag>text</tag> or
<tag/>
– Tags must be
properly balanced
and nested
– Must have a root tag
<webapp>
Welcome Page Properties
• Page shown at webapp startup
– Default: index.jsp
Code in web.xml
that does the servlet
mapping
Initial Parameters
• Global constants stored in web.xml
– Better than “hardwiring” values directly into the code
• Easier for nonprogrammer to modify
– Form like parameter: name/value
• Example: “price per unit” in widget site
web.xml
Java servlet class
pricePerUnit=9.95 that needs widget cost
configuration-level parameters
AnotherServlet‘s configuration
• Context level AnothereServlet object
– General to all server
configuration-level parameters
pages/servlets
context-level parameters
Configuration Parameters