Server-side  Web Programming Lecture 6:  Java Servlets and the  web.xml  Configuration File
Configuration Files <ul><li>Each webapp has a  web.xml  file </li></ul><ul><ul><li>In  WEB-INF  subdirectory </li></ul></u...
The  web.xml  file <ul><li>Complex (and finicky) XML </li></ul><ul><li>If buggy, application  will not load </li></ul><ul>...
The  web.xml  file <ul><li>Internally, still XML </li></ul><ul><ul><li>Can view and edit at XML tab </li></ul></ul><ul><ul...
Welcome Page Properties <ul><li>Page shown at webapp startup </li></ul><ul><ul><li>Default: index.jsp </li></ul></ul><ul><...
New Welcome Page <ul><li>Will go to that page when webapp started </li></ul><ul><li>Note change in web.xml </li></ul>
Servlet Mapping <ul><li>Web.xml links  referrer page  to be linked with  actual servlet class </li></ul><ul><li>Mapping fr...
Servlet Mapping No such file – this is just a url pattern that will refer to an actual servlet via web.xml Added to the ur...
Servlet Mapping Note that the url pattern appears in the browser Code in web.xml that does the servlet mapping
Initial Parameters <ul><li>Global constants  stored in web.xml </li></ul><ul><ul><li>Better than “hardwiring” values direc...
Initial Parameters <ul><li>Configuration level </li></ul><ul><ul><li>Specific to  single  servlet </li></ul></ul><ul><li>C...
Configuration Parameters Can create in servlet tab
Configuration Parameters Form of xml tag
Configuration Parameters <ul><li>Accessing from servlet: </li></ul><ul><ul><li>Get the  servlet configuration ServletConfi...
Context Parameters Can create in general tab
Context Parameters Form of xml tag
Configuration Parameters <ul><li>Accessing from servlet: </li></ul><ul><ul><li>Get the  servlet configuration ServletConfi...
Configuration Parameters
Upcoming SlideShare
Loading in...5
×

Lecture6

497
-1

Published on

Published in: Technology, Education
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total Views
497
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
8
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Lecture6

  1. 1. Server-side Web Programming Lecture 6: Java Servlets and the web.xml Configuration File
  2. 2. Configuration Files <ul><li>Each webapp has a web.xml file </li></ul><ul><ul><li>In WEB-INF subdirectory </li></ul></ul><ul><li>Defines: </li></ul><ul><ul><li>Mapping from servlet names to servlet files </li></ul></ul><ul><ul><li>Global constants </li></ul></ul><ul><ul><li>Default error pages </li></ul></ul><ul><ul><li>Security roles… </li></ul></ul>
  3. 3. The web.xml file <ul><li>Complex (and finicky) XML </li></ul><ul><li>If buggy, application will not load </li></ul><ul><ul><li>Errors displayed in Tomcat window when started </li></ul></ul><ul><li>NetBeans provides interface to manipulate its values </li></ul>
  4. 4. The web.xml file <ul><li>Internally, still XML </li></ul><ul><ul><li>Can view and edit at XML tab </li></ul></ul><ul><ul><li>Basic form of XML: <tag>text</tag> or <tag/> </li></ul></ul><ul><ul><li>Tags must be properly balanced and nested </li></ul></ul><ul><ul><li>Must have a root tag <webapp> </li></ul></ul>
  5. 5. Welcome Page Properties <ul><li>Page shown at webapp startup </li></ul><ul><ul><li>Default: index.jsp </li></ul></ul><ul><li>Can change to a new welcome file </li></ul>
  6. 6. New Welcome Page <ul><li>Will go to that page when webapp started </li></ul><ul><li>Note change in web.xml </li></ul>
  7. 7. Servlet Mapping <ul><li>Web.xml links referrer page to be linked with actual servlet class </li></ul><ul><li>Mapping from a name to a java class </li></ul><ul><ul><li>Allows servlet code to be changed without having to rewrite other pages </li></ul></ul><ul><ul><ul><li>Important since name of class = name of file in Java </li></ul></ul></ul><ul><li>Mapping from a url pattern to a servlet name </li></ul><ul><ul><li>Allows servlets to be “hidden” from user </li></ul></ul>Referring file that invokes servlet url pattern used in FORM ACTION web.xml url pattern  name name  servlet class Java servlet class with actual code
  8. 8. 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
  9. 9. Servlet Mapping Note that the url pattern appears in the browser Code in web.xml that does the servlet mapping
  10. 10. Initial Parameters <ul><li>Global constants stored in web.xml </li></ul><ul><ul><li>Better than “hardwiring” values directly into the code </li></ul></ul><ul><ul><ul><li>Easier for nonprogrammer to modify </li></ul></ul></ul><ul><ul><li>Form like parameter: name/value </li></ul></ul><ul><ul><ul><li>Example: “price per unit” in widget site </li></ul></ul></ul><ul><ul><li>Note that such information is usually in a database </li></ul></ul><ul><ul><li>However, the name of the database is often an initial parameter! </li></ul></ul>web.xml pricePerUnit=9.95 Java servlet class that needs widget cost
  11. 11. Initial Parameters <ul><li>Configuration level </li></ul><ul><ul><li>Specific to single servlet </li></ul></ul><ul><li>Context level </li></ul><ul><ul><li>General to all server pages/servlets </li></ul></ul>overall application context … context-level parameters SomeServlet ‘s configuration configuration-level parameters SomeServlet object AnotherServlet ‘s configuration configuration-level parameters AnothereServlet object
  12. 12. Configuration Parameters Can create in servlet tab
  13. 13. Configuration Parameters Form of xml tag
  14. 14. Configuration Parameters <ul><li>Accessing from servlet: </li></ul><ul><ul><li>Get the servlet configuration ServletConfig config = getServletConfig(); </li></ul></ul><ul><ul><li>Get the value corresponding to the name of the parameter String value = config.getInitParameter(“ name &quot;); </li></ul></ul>
  15. 15. Context Parameters Can create in general tab
  16. 16. Context Parameters Form of xml tag
  17. 17. Configuration Parameters <ul><li>Accessing from servlet: </li></ul><ul><ul><li>Get the servlet configuration ServletConfig config = getServletConfig(); </li></ul></ul><ul><ul><li>Get the webapp context from the configuration ServletContext context = config.getServletContext(); </li></ul></ul><ul><ul><li>Get the value corresponding to the name of the parameter String value = context.getInitParameter(“ name &quot;); </li></ul></ul><ul><li>Accessing from a JSP: </li></ul><ul><ul><li>String value = application.getInitParameter(“ name &quot;); </li></ul></ul>Built-in object in JSP (like “request”)
  18. 18. Configuration Parameters
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×