部署描述符文件就像所有xml文件一樣,必須以一個(gè)XML頭開(kāi)始。這個(gè)頭聲明可以使用的XML版本并給出文件的字符編碼。 DOCYTPE聲明必須立即出現(xiàn)在此頭之后。這個(gè)聲明告訴服務(wù)器適用的servlet規(guī)范的版本(如2.2或2.3)并指定治理此文件其余部分內(nèi)容的語(yǔ)法的DTD(Document Type Definition,文檔類(lèi)型定義)。 所有部署描述符文件的頂層(根)元素為web-app。請(qǐng)注重,XML元素不像Html,他們是大小寫(xiě)敏感的。因此,web-App和WEB-APP都是不合法的,web-app必須用小寫(xiě)。
2 部署描述符文件內(nèi)的元素次序
XML元素不僅是大小寫(xiě)敏感的,而且它們還對(duì)出現(xiàn)在其他元素中的次序敏感。例如,XML頭必須是文件中的第一項(xiàng),DOCTYPE聲明必須是第二項(xiàng),而web-app元素必須是第三項(xiàng)。在web-app元素內(nèi),元素的次序也很重要。服務(wù)器不一定強(qiáng)制要求這種次序,但它們答應(yīng)(實(shí)際上有些服務(wù)器就是這樣做的)完全拒絕執(zhí)行含有次序不正確的元素的Web應(yīng)用。這表示使用非標(biāo)準(zhǔn)元素次序的web.xml文件是不可移植的。 下面的列表給出了所有可直接出現(xiàn)在web-app元素內(nèi)的合法元素所必需的次序。例如,此列表說(shuō)明servlet元素必須出現(xiàn)在所有servlet-mapping元素之前。請(qǐng)注重,所有這些元素都是可選的。因此,可以省略掉某一元素,但不能把它放于不正確的位置。 l icon icon元素指出IDE和GUI工具用來(lái)表示W(wǎng)eb應(yīng)用的一個(gè)和兩個(gè)圖像文件的位置。 l display-name display-name元素提供GUI工具可能會(huì)用來(lái)標(biāo)記這個(gè)特定的Web應(yīng)用的一個(gè)名稱(chēng)。 l description description元素給出與此有關(guān)的說(shuō)明性文本。 l context-param context-param元素聲明應(yīng)用范圍內(nèi)的初始化參數(shù)。 l filter 過(guò)濾器元素將一個(gè)名字與一個(gè)實(shí)現(xiàn)javax.servlet.Filter接口的類(lèi)相關(guān)聯(lián)。 l filter-mapping 一旦命名了一個(gè)過(guò)濾器,就要利用filter-mapping元素把它與一個(gè)或多個(gè)servlet或jsp頁(yè)面相關(guān)聯(lián)。 l listener servlet API的版本2.3增加了對(duì)事件監(jiān)聽(tīng)程序的支持,事件監(jiān)聽(tīng)程序在建立、修改和刪除會(huì)話(huà)或servlet環(huán)境時(shí)得到通知。Listener元素指出事件監(jiān)聽(tīng)程序類(lèi)。 l servlet 在向servlet或JSP頁(yè)面制定初始化參數(shù)或定制URL時(shí),必須首先命名servlet或JSP頁(yè)面。Servlet元素就是用來(lái)完成此項(xiàng)任務(wù)的。 l servlet-mapping 服務(wù)器一般為servlet提供一個(gè)缺省的URL:http://host/webAppPRefix/servlet/ServletName。但是,經(jīng)常會(huì)更改這個(gè)URL,以便servlet可以訪問(wèn)初始化參數(shù)或更輕易地處理相對(duì)URL。在更改缺省URL時(shí),使用servlet-mapping元素。 l session-config 假如某個(gè)會(huì)話(huà)在一定時(shí)間內(nèi)未被訪問(wèn),服務(wù)器可以?huà)仐壦怨?jié)省內(nèi)存??赏ㄟ^(guò)使用HttpSession的setMaxInactiveInterval方法明確設(shè)置單個(gè)會(huì)話(huà)對(duì)象的超時(shí)值,或者可利用session-config元素制定缺省超時(shí)值。 l mime-mapping 假如Web應(yīng)用具有想到非凡的文件,希望能保證給他們分配特定的MIME類(lèi)型,則mime-mapping元素提供這種保證。 l welcom-file-list welcome-file-list元素指示服務(wù)器在收到引用一個(gè)目錄名而不是文件名的URL時(shí),使用哪個(gè)文件。 l error-page error-page元素使得在返回特定HTTP狀態(tài)代碼時(shí),或者特定類(lèi)型的異常被拋出時(shí),能夠制定將要顯示的頁(yè)面。 l taglib taglib元素對(duì)標(biāo)記庫(kù)描述符文件(Tag Libraryu Descriptor file)指定別名。此功能使你能夠更改TLD文件的位置,而不用編輯使用這些文件的JSP頁(yè)面。 l resource-env-ref resource-env-ref元素聲明與資源相關(guān)的一個(gè)治理對(duì)象。 l resource-ref resource-ref元素聲明一個(gè)資源工廠使用的外部資源。 l security-constraint security-constraint元素制定應(yīng)該保護(hù)的URL。它與login-config元素聯(lián)合使用 l login-config 用login-config元素來(lái)指定服務(wù)器應(yīng)該怎樣給試圖訪問(wèn)受保護(hù)頁(yè)面的用戶(hù)授權(quán)。它與sercurity-constraint元素聯(lián)合使用。 l security-role security-role元素給出安全角色的一個(gè)列表,這些角色將出現(xiàn)在servlet元素內(nèi)的security-role-ref元素的role-name子元素中。分別地聲明角色可使高級(jí)IDE處理安全信息更為輕易。 l env-entry env-entry元素聲明Web應(yīng)用的環(huán)境項(xiàng)。 l ejb-ref ejb-ref元素聲明一個(gè)EJB的主目錄的引用。 l ejb-local-ref ejb-local-ref元素聲明一個(gè)EJB的本地主目錄的應(yīng)用。
/** Simple servlet used to illustrate servlet naming * and custom URLs. * <P> * Taken from More Servlets and JavaServer Pages * from Prentice Hall and Sun Microsystems Press, * http://www.moreservlets.com/. * ? 2002 Marty Hall; may be freely used or adapted. */
public class TestServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String uri = request.getRequestURI(); out.println(ServletUtilities.headWithTitle("Test Servlet") + "<BODY BGCOLOR="#FDF5E6"> " + "<H2>URI: " + uri + "</H2> " + "</BODY></HTML>"); } }
程序清單5-2 web.xml(說(shuō)明servlet名稱(chēng)的摘錄) <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
/** Simple servlet used to give error messages to * users who try to access default servlet URLs * (i.e., http://host/webAppPrefix/servlet/ServletName) * in Web applications that have disabled this * behavior. * <P> * Taken from More Servlets and JavaServer Pages * from Prentice Hall and Sun Microsystems Press, * http://www.moreservlets.com/. * ? 2002 Marty Hall; may be freely used or adapted. */
public class SorryServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Invoker Servlet Disabled."; out.println(ServletUtilities.headWithTitle(title) + "<BODY BGCOLOR="#FDF5E6"> " + "<H2>" + title + "</H2> " + "Sorry, access to servlets by means of " + "URLs that begin with " + "http://host/webAppPrefix/servlet/ " + "has been disabled. " + "</BODY></HTML>"); }
/** Simple servlet used to illustrate servlet * initialization parameters. * <P> * Taken from More Servlets and JavaServer Pages * from Prentice Hall and Sun Microsystems Press, * http://www.moreservlets.com/. * ? 2002 Marty Hall; may be freely used or adapted. */
public class InitServlet extends HttpServlet { private String firstName, emailAddress;
/** Simple filter that prints a report on the standard output * whenever the associated servlet or JSP page is accessed. * <P> * Taken from More Servlets and JavaServer Pages * from Prentice Hall and Sun Microsystems Press, * http://www.moreservlets.com/. * ? 2002 Marty Hall; may be freely used or adapted. */
public class ReportFilter implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { HttpServletRequest req = (HttpServletRequest)request; System.out.println(req.getRemoteHost() + " tried to access " + req.getRequestURL() + " on " + new Date() + "."); chain.doFilter(request,response); }
public void init(FilterConfig config) throws ServletException { }
越來(lái)越多的開(kāi)發(fā)環(huán)境開(kāi)始提供servlet和JSP的直接支持。例子有Borland Jbuilder Enterprise Edition、Macromedia UltraDev、Allaire JRun Studio(寫(xiě)此文時(shí),已被Macromedia收購(gòu))以及IBM VisuaAge for Java等。 大量的web.xml元素不僅是為服務(wù)器設(shè)計(jì)的,而且還是為可視開(kāi)發(fā)環(huán)境設(shè)計(jì)的。它們包括icon、display-name和discription等。 可回憶一下,在web.xml內(nèi)以適當(dāng)?shù)卮涡蚵暶鱳eb-app子元素很重要。不過(guò),這里只要記住icon、display-name和description是web.xml的web-app元素內(nèi)的前三個(gè)合法元素即可。 l icon icon元素指出GUI工具可用來(lái)代表Web應(yīng)用的一個(gè)和兩個(gè)圖像文件。可利用small-icon元素指定一幅16 x 16的GIF或JPEG圖像,用large-icon元素指定一幅32 x 32的圖像。下面舉一個(gè)例子: <icon> <small-icon>/images/small-book.gif</small-icon> <large-icon>/images/tome.jpg</large-icon> </icon> l display-name display-name元素提供GUI工具可能會(huì)用來(lái)標(biāo)記此Web應(yīng)用的一個(gè)名稱(chēng)。下面是個(gè)例子。 <display-name>Rare Books</display-name> l description description元素提供解釋性文本,如下所示: <description> This Web application represents the store developed for rare-books.com, an online bookstore specializing in rare and limited-edition books. </description>
/** Simple listener that prints a report on the standard output * when the ServletContext is created or destroyed. * <P> * Taken from More Servlets and JavaServer Pages * from Prentice Hall and Sun Microsystems Press, * http://www.moreservlets.com/. * ? 2002 Marty Hall; may be freely used or adapted. */
public class ContextReporter implements ServletContextListener { public void contextInitialized(ServletContextEvent event) { System.out.println("Context created on " + new Date() + "."); }
public void contextDestroyed(ServletContextEvent event) { System.out.println("Context destroyed on " + new Date() + "."); } }
程序清單5-21 web.xml(聲明一個(gè)監(jiān)聽(tīng)程序的摘錄) <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">