0% found this document useful (0 votes)
8 views

Server-Side Web Programming

The document discusses the web.xml configuration file that defines mappings from servlet names to classes, global constants, default error pages, and security roles for a Java web application. It describes how the web.xml file uses XML tags to configure properties like the welcome page, servlet mappings that link URLs to servlets, and initial parameters that define global values accessible to servlets. Errors may occur if the web.xml file contains bugs, so it is important to properly structure the XML tags and ensure all elements are balanced and nested correctly.

Uploaded by

Hop Huynh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Server-Side Web Programming

The document discusses the web.xml configuration file that defines mappings from servlet names to classes, global constants, default error pages, and security roles for a Java web application. It describes how the web.xml file uses XML tags to configure properties like the welcome page, servlet mappings that link URLs to servlets, and initial parameters that define global values accessible to servlets. Errors may occur if the web.xml file contains bugs, so it is important to properly structure the XML tags and ensure all elements are balanced and nested correctly.

Uploaded by

Hop Huynh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Server-side

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

• Can change to a new welcome file


New Welcome Page
• Will go to that page when webapp started

• Note change in web.xml


Servlet Mapping
Web.xml links referrer page to be linked with actual servlet
class
• Mapping from a name to a java class
– Allows servlet code to be changed without having to rewrite
other pages
• Important since name of class = name of file in Java
• Mapping from a url pattern to a servlet name
– Allows servlets to be “hidden” from user

Referring file that web.xml


invokes servlet
Java servlet class
url pattern  name
with actual code
url pattern used
in FORM ACTION
name  servlet class
Servlet Mapping
No such file – this is
just a url pattern that
will refer to an actual
servlet via web.xml

Added to the url


patterns that map to
this servlet
Servlet Mapping
Note that the url
pattern appears in
the browser

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

– Note that such information is usually in a database


– However, the name of the database is often an initial
parameter!
Initial Parameters
• Configuration level overall application context

– Specific to single SomeServlet‘s configuration

servlet SomeServlet object

configuration-level parameters

AnotherServlet‘s configuration
• Context level AnothereServlet object
– General to all server
configuration-level parameters
pages/servlets

context-level parameters
Configuration Parameters

Can create in servlet tab


Configuration Parameters

Form of xml tag


Configuration Parameters
• Accessing from servlet:
– Get the servlet configuration
ServletConfig config = getServletConfig();
– Get the value corresponding to the name of the parameter
String value = config.getInitParameter(“name");
Context Parameters

Can create in general tab


Context Parameters

Form of xml tag


Configuration Parameters
• Accessing from servlet:
– Get the servlet configuration
ServletConfig config = getServletConfig();
– Get the webapp context from the configuration
ServletContext context = config.getServletContext();
– Get the value corresponding to the name of the parameter
String value = context.getInitParameter(“name");
• Accessing from a JSP:
– String value = application.getInitParameter(“name");

Built-in object in JSP


(like “request”)
Configuration Parameters

You might also like