1. 程式人生 > >訪問 WEB-INF 下的 jsp 和 html

訪問 WEB-INF 下的 jsp 和 html

因為web-inf下,應用伺服器把它指為禁訪目錄,即直接在瀏覽器裡是不能訪問到的。但是可以讓servlet進行訪問,

如web-inf下有a.jsp則可以用request.getRequestDispatcher("/WEB-INF/a.jsp").forward(request,response);如果想訪問web-inf下的html檔案的話,用request.getRequestDispatcher("/WEB-INF/a.html").forward(request,response);是訪問不了的。原因很簡單,jsp就是servlet,會被編譯成class檔案,而html的就不行了。 所以需要配置以下conf下的web.xml檔案才能去訪問html。 

具體實現如下: 用開啟tomcat安裝目錄下conf下的web.xml檔案,找到 

<servlet-mapping> 

<servlet-name>jsp</servlet-name> 

<url-pattern>*.jsp</url-pattern> 

</servlet-mapping> 

然後在它下面新增 

</servlet-mapping> 

<servlet-mapping> 

<servlet-name>jsp</servlet-name> 

<url-pattern>*.html</url-pattern> 

</servlet-mapping> 

這樣的話,就能用request.getRequestDispatcher("/WEB-INF/a.html").forward(request,response);去訪問web-inf下的html了