|
This question
will help gauge a candidate's understanding of a fundamental
concept of the current J2EE API, the servlet context. A good
understanding of the context path is required for all JSP
development, and thus this question is appropriate for both
beginners and expert developers alike.
What is the servlet context, and how does it affect the design
of JSP pages, servlets, and HTML?
When a J2EE web
application is deployed, it is assigned a context path by
its container. The context path is the root URL path of the
application on the server. All requests targeted at the application's
root URL will be passed to the application in question. Within
the application, all paths are assumed to be relative to the
context path. This allows the application to be deployed on
any path deemed appropriate by the container or deployment
manager.
This context path
assumption applies only to calls into the servlet API however.
It is the responsibility of the web developer to assure that
any URL references external to the API are made relative to
the appropriate context path. This includes anchor tags (links)
in an HTML page as well as references to images, style sheets,
applets and other content. While hard coding the context path
into the HTML will work, a good developer will recognize that
this practice will limit the flexibility of deployment for
the application, and is undesirable. The scriptlet (or
an equivalent custom tag) should be used dynamically prepend
the appropriate context path to any URL references. Some servlet
containers will provide the mappings for you at run-time,
all be it with a decrease in performance. It is important
to note that the context path does not include the protocol,
port number, or name of the web server. Thus an application
accessible at the URL http://www.example.com/myapp has a context
path of simply /myapp, not the entire URL.
About
the author
Duane Fields is a Java developer, author, and Internet technologist
with nearly a decade of professional experience in the design
and development of leading edge Internet products and services.
Duane is also a respected member of the Java development community
and is frequently invited to speak at industry conferences
and events. He has co-authored two books and published numerous
articles on many aspects of web application development from
Java to Relational Databases. The newly expanded, second edition
of his best selling book "Web Development with JavaServer
Pages" was released in December of 2001.
|