使用場景:java
在現實項目中,偶爾須要在 跳轉 指定的 項目首頁以前作一些處理,可是又不想 單獨多加一個 跳轉頁面來完成這次操做;如此,該如何處理呢?web
這裏,能夠 利用 web.xml 中的 welcome-file-list 設置默認 來 處理。具體操做以下面的例子所示:app
注意: welcome-file 節點 須要 與 servlet-mapping 中的 url-pattern 節點 內容保持一致.this
一、web.xml url
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>web_test</display-name> <welcome-file-list> <welcome-file>emp_tz.hts</welcome-file> </welcome-file-list> <servlet> <servlet-name>FrameControlServlet</servlet-name> <servlet-class>com.myweb.test.MyDefaultManagerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FrameControlServlet</servlet-name> <url-pattern>/emp_tz.hts</url-pattern> </servlet-mapping> </web-app>
二、對應 servlet 處理spa
package com.myweb.test; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyDefaultManagerServlet extends HttpServlet{ private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("this is doPost..."); response.getWriter().write("very ok"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("this is doGet..."); response.getWriter().write("ok"); } }