2014-01-07

GAE 的 static file 不會觸發 filter?

一直以來 JSP Filter 用得好好的,結果前幾天一直被炸, index.html 始終不會觸發 Filter.doFilter(); 但是只要換成 JSP 或是 Servlet 就沒事,正常得很。

本來懷疑是 GPE 的問題,結果丟上 GAE 也是一樣的情況。 後來重開乾淨的純 GWT project 跟純 GAE project 比對, 發現只要有用 GAE SDK 的 project 就會有這個現象, 看起來就不像是 bug 了 XD

胡亂繞了一下,終於找到癥結點了。 首先是 GAE 文件講到 web.xml 時說:

Note: Static files, files that are served verbatim to users such as images, CSS or JavaScript, are handled separately from paths mentioned in the deployment descriptor. A request for a URL path that matches a path to a file in the WAR that’s considered a static file will serve the file, regardless of servlet and filter mappings in the deployment descriptor. You can exclude files from those treated as static files using the appengine-web.xml file.

搭配 appengine-web.xml 的這段一起看:

These are known as static files because they do not change, and can benefit from web servers dedicated just to static content. App Engine serves static files from dedicated servers and caches that are separate from the application servers.

[…]

By default, all files in the WAR are treated as both static files and resource files, except for JSP files, which are compiled into servlet classes and mapped to URL paths, and files in the WEB-INF/ directory, which are never served as static files and always available to the app as resource files.

簡單地說,就是除了 JSP 跟 WEB-INF 底下的東西,其他預設都會當成 static file。 而 static file 因為都不會變、所以可以作一些特別處理來優化效能。 所以 index.html 始終不觸發 doFilter(),也就完全合情合理了。 於是只要照著把 index.html 排除在外,就會觸發 doFilter() 了:

<static-files>
    <exclude path="index.html" />
</static-files>

把人家刻意優化的東西當成 bug,實在太丟臉了。 所以這篇貼在 PT2Club 就算了,不要拿到 DontCare Blog 上丟人現眼 [遮臉]

(謎之聲:干有差? [指])