xml发生错误_WEB之web.xml详解
web.xml的作用
web.xml,一個(gè)Tomcat工程中最重要的配置文件。web.xml沒有其實(shí)也可以----只要你確定你的項(xiàng)目里面不需要任何過濾器、監(jiān)聽器、Servlet等等。我試了一下,沒有web.xml對(duì)那些已經(jīng)編譯成Servlet的jsp頁面來說,是不影響正常顯示的,但是那些沒有編譯成Servlet的jsp頁面,訪問的時(shí)候就會(huì)報(bào)500的錯(cuò)誤了。下面逐一看一下web.xml里常用標(biāo)簽的作用。
welcome-file-list
這個(gè)標(biāo)簽是用來配置首頁用的:
index1.jspindex2.jspindex3.jspindex4.jsp/target/redirectAndFoward.jsp這么配置的意思,就是當(dāng)用戶訪問http://ip:port/工程名的時(shí)候,會(huì)根據(jù)welcome-file-list配置的頁面列表,從項(xiàng)目的根目錄開始找頁面:
1、第一個(gè)配置的index1.jsp能找到,就展示index1.jsp
2、找不到index1.jsp,則去找第二個(gè)index2.jsp,index2.jsp能找到就展示index2.jsp,
3、找不到index3.jsp,則去找第三個(gè)index3.jsp,以此類推,如果所有的頁面都找不到則報(bào)HTTP Status 404即頁面找不到
注意一下,像配置的最后一個(gè)welcome-file這種寫法也是支持的,我試了一下最前面的那個(gè)"/"可加可不加
error-page
error-page表示當(dāng)HTTP返回指定狀態(tài)碼的時(shí)候,容器將此次請(qǐng)求轉(zhuǎn)發(fā)到配置的指定頁面:
400/filter/error.jsp404/filter/error.jsp500/filter/error.jsp這表示HTTP狀態(tài)碼為400、404、500的時(shí)候,此次請(qǐng)求都會(huì)被轉(zhuǎn)發(fā)到http://ip:port/工程名/filter/error.jsp這個(gè)頁面上去。注意一下這里是error-code,所以如果是200的話,是沒有效果的
filter
filter就不說了,兩種include方式及filter中的dispatcher解析一文已經(jīng)講得很詳細(xì)了,filter的寫法也在上面。
另外注意一點(diǎn),其實(shí)大家也都知道,提一下:filter執(zhí)行的順序就是filter定義的順序。
servlet
servlet開發(fā)者比較熟悉,先匹配規(guī)則,匹配到路徑后走相應(yīng)的Servlet類,就不說了。下面配一個(gè)相對(duì)不那么常用的,只是相對(duì)而已,這種servlet的寫法很常見:
startUpServletcom.xrq.servlet.StartUpServletName123Age4568這是一個(gè)啟動(dòng)servlet,表示容器啟動(dòng)的時(shí)候servlet啟動(dòng),調(diào)用其init()方法,所以首先第一個(gè)標(biāo)簽load-on-start,分幾點(diǎn)說:
1、load-on-startup元素標(biāo)記容器是否在啟動(dòng)的時(shí)候就加載這個(gè)servlet(實(shí)例化并調(diào)用其init方法)
2、它的值必須是一個(gè)整數(shù),表示servlet應(yīng)該被載入的順序
3、當(dāng)值為0或者大于0時(shí),表示容器在應(yīng)用啟動(dòng)時(shí)就加載并初始化這個(gè)servlet
4、當(dāng)值小于0或者沒有指定時(shí),表示這個(gè)容器在該servlet被選擇時(shí)才會(huì)去加載
5、正數(shù)值越小,該servlet的優(yōu)先級(jí)就越高,應(yīng)用啟動(dòng)時(shí)就越先加載
6、當(dāng)值相同時(shí),容器自己選擇順序來加載
所以,load-on-startup中配置了一個(gè)大于等于0的正整數(shù)時(shí),該servlet可以當(dāng)作一個(gè)普通的servlet來用,無非是這個(gè)servlet啟動(dòng)的時(shí)候會(huì)加載其init()方法罷了。
另外一個(gè)就是init-param了,表示一個(gè)鍵值對(duì),只能在本servlet里面被使用,通過ServletConfig獲取,StartUpServlet的寫法是:
public class StartUpServlet extends HttpServlet{ /** * 序列化 */ private static final long serialVersionUID = 1L; public void init() throws ServletException { System.out.println("StartUpServlet.init()"); System.out.println("Name:" + getServletConfig().getInitParameter("Name")); System.out.println("Age:" + getServletConfig().getInitParameter("Age")); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } public void destroy() { System.out.println("StartUpServlet.destory()"); }}servlet只能取到配置在自己的標(biāo)簽內(nèi)的。
listener
listener即監(jiān)聽器,是Servlet的監(jiān)聽器,它可以監(jiān)聽客戶端的請(qǐng)求、服務(wù)器端的操作等,在事情發(fā)生前后做一些必要的處理。通過監(jiān)聽器,可以自動(dòng)激發(fā)一些操作,比如監(jiān)聽在線用戶數(shù)量,下面就寫一個(gè)監(jiān)聽用戶數(shù)量的監(jiān)聽器,首先web.xml配置很簡單:
com.xrq.listener.UserCounterListener寫一個(gè)監(jiān)聽器,監(jiān)聽用戶數(shù)量一般都是以session創(chuàng)建和session失效為依據(jù)的,所以實(shí)現(xiàn)HttpSessionListener:
public class UserCounterListener implements HttpSessionListener{ private AtomicInteger ai = new AtomicInteger(0); public void sessionCreated(HttpSessionEvent se) { ai.incrementAndGet(); } public void sessionDestroyed(HttpSessionEvent se) { ai.decrementAndGet(); } public int getUserCount() { return ai.get(); }}除了監(jiān)聽session的監(jiān)聽器外,再介紹一些別的監(jiān)聽器接口:
1、ServletContextListener
用于監(jiān)聽WEB引用啟動(dòng)和銷毀的事件,SevletContextListener是ServletContext的監(jiān)聽者,如果ServletContext發(fā)生變化,如服務(wù)器啟動(dòng)、服務(wù)器關(guān)閉,都會(huì)被ServletContextListener監(jiān)聽到。監(jiān)聽事件為ServletContextEvent
2、ServletContextAttributeListener
用于監(jiān)聽WEB應(yīng)用屬性改變的事件,包括添加屬性、刪除屬性、修改屬性。監(jiān)聽時(shí)間為ServletContextAttributeEvent
3、HttpSessionBindingListener
HttpSessionBindingListener是唯一一個(gè)不需要在web.xml中配置的Listener,當(dāng)我們的類實(shí)現(xiàn)了HttpSessionBindListener接口后,只要對(duì)象加入session或者從session中移除,容器會(huì)分別自動(dòng)調(diào)用以下兩個(gè)方法:
(1)void valueBound(HttpSesssionBindEvent event)
(2)void valueUnbound(HttpSessionBindingEvent event)
注意,這個(gè)監(jiān)聽器的觸發(fā)是針對(duì)于實(shí)現(xiàn)了該監(jiān)聽器的類的,只有把實(shí)現(xiàn)了該監(jiān)聽器的類set進(jìn)session或從session中remove才會(huì)觸發(fā)這個(gè)監(jiān)聽器
4、HttpSessionAttributeListener
用于監(jiān)聽HttpSession中的屬性的操作,當(dāng)session里面增加一個(gè)屬性時(shí),觸發(fā)attributeAdded(HttpSessionBindEvent se)方法;當(dāng)在session中刪除一個(gè)屬性時(shí),觸發(fā)attributeRemoved(HttpSessionBindEvent se)方法;當(dāng)session屬性被重新設(shè)置時(shí),觸發(fā)attributeReplaced(HttpSessionBindingEvent se)方法。
注意,這個(gè)監(jiān)聽器的觸發(fā)是針對(duì)所有的session的,只要session的屬性發(fā)生變化,都會(huì)觸發(fā)這個(gè)監(jiān)聽器
5、HttpSessionListener
這個(gè)上面已經(jīng)寫過了,監(jiān)聽HttpSession。當(dāng)創(chuàng)建一個(gè)session時(shí),觸發(fā)sessionCreated(HttpSessionEvent se)方法;當(dāng)銷毀一個(gè)session時(shí),會(huì)觸發(fā)sessionDestoryed(HttpSessionEvent se)方法
6、HttpSessionActivationListener
這個(gè)用得不太多,主要監(jiān)聽同一個(gè)session轉(zhuǎn)移至不同的JVM的情形
7、ServletRequestListener和ServletRequestAttributeListener
和ServletContextListener和ServletContextAttributeListener類似,前者監(jiān)聽Request的創(chuàng)建和銷毀、后者監(jiān)聽Request中屬性的增刪改
context-param
context-param里面配置的鍵值對(duì)是全局共享的,整個(gè)web項(xiàng)目都能取到這個(gè)上下文,比方說我在web.xml里面配置了一個(gè)HTTP端口和一個(gè)HTTPS端口:
NotSSLPort8080SSLPort8443servlet可以這么取:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ System.out.println("NotSSLPort:" + getServletContext().getInitParameter("NotSSLPort")); System.out.println("SSLPort:" + getServletContext().getInitParameter("SSLPort"));}filter可以這么取:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException{ HttpServletRequest req = (HttpServletRequest)request; ServletContext sc = req.getSession().getServletContext(); System.out.println("NotSSLPort:" + sc.getInitParameter("NotSSLPort")); System.out.println("SSLPort:" + sc.getInitParameter("SSLPort")); chain.doFilter(request, response);}listener可以這么取,以ServletContextListener為例:
public void contextInitialized(ServletContextEvent sce){ System.out.println("Enter SCListener.contextInitialized"); System.out.println("NotSSLPort:" + sce.getServletContext().getInitParameter("NotSSLPort")); System.out.println("SSLPort:" + sce.getServletContext().getInitParameter("SSLPort"));}反正最終的目的就是取到一個(gè)ServletContext就對(duì)了。是不是感覺ServletContext很熟悉呢?沒錯(cuò),看一下jsp默認(rèn)的內(nèi)置對(duì)象,隨便打開一個(gè)轉(zhuǎn)換成Servlet的jsp頁面,里面都有內(nèi)置對(duì)象的定義:
PageContext pageContext = null;HttpSession session = null;ServletContext application = null;ServletConfig config = null;JspWriter out = null;Object page = this;JspWriter _jspx_out = null;PageContext _jspx_page_context = nullServletContext也就是我們常說的Application
可能不太常見,這個(gè)標(biāo)簽是用來指定對(duì)應(yīng)的格式的瀏覽器處理方式的,添加mime-type的映射,就可以避免某些類型的文件直接在瀏覽器中打開了。
舉個(gè)例子:
docapplication/mswordpdfapplication/pdfrarapplication/x-rar-compressedtxttext/plainxlsapplication/vnd.ms-excel這就指定了.doc、pdf、rar、txt、xls這五種類型的文件的打開方式了。常見的MIME類型有:
session-config
session-config是用來配置session失效時(shí)間的,因?yàn)閟ession-config里面只有一個(gè)子標(biāo)簽:
30以分鐘為單位。當(dāng)然,代碼里面也可以設(shè)置:
"request.getSession.setMaxInactiveInterval(30 * 60);"就可以了,單位為秒。
元素加載順序
首先可以肯定,加載順序與它們?cè)趙eb.xml文件中的先后順序無關(guān),即不會(huì)因?yàn)閒ilter寫在listener前面就先加載filter。最終得出的結(jié)論是listener->filter->servlet。
然后是context-param,用于向ServletContext提供鍵值對(duì),即應(yīng)用程序上下文信息,listener、filter、servlet都可能會(huì)用到這些上下文中的信息,那么context-param是否應(yīng)該寫在listener、filter、servlet前面呢?未必,context-param可以寫在任何位置,因此真正的加載順序應(yīng)該是:context-param->listener->filter->servlet。
總結(jié)
以上是生活随笔為你收集整理的xml发生错误_WEB之web.xml详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mongodb 事务_MongoDB4
- 下一篇: springboot mybatis 事