2012-07-18 60 views
2

我遇到了應用引擎上的servlet映射問題。Servlet映射適用於本地,但不適用於應用引擎中的網絡服務器

詳細
用我的web.xml配置文件我映射URL模式「/我」重定向到info.html裏對我的WAR路徑的根目錄。

當我在本地運行並訪問url localhost:8888/i時,Web服務器正確地重定向到info.html站點。但是,當我推到App Engine和試圖訪問www.mysite.com/i我得到一個404 錯誤:NOT_FOUND

問題
1.爲什麼我的服務器映射工作本地而不是遠程? 2.有沒有另外一種方法可以將像www.mysite.com/i這樣的url模式映射到我的戰爭路徑中的靜態文件?

*雖然我在應用程序引擎上,但我會認爲這與通過web.xml使用servlet映射的其他Java服務器類似。另外我知道映射到一個靜態html文件可能看起來很奇怪,但目前我沒有加載任何動態內容。

代碼示例
下面我供您參考我的web.xml文件的下調版本。它隔離我如何做重定向。

<?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" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 

    <servlet> 
     <servlet-name>Info</servlet-name> 
     <jsp-file>info.html</jsp-file> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>Info</servlet-name> 
     <url-pattern>/i</url-pattern> 
    </servlet-mapping> 

    <welcome-file-list> 
     <welcome-file>index.html</welcome-file> 
    </welcome-file-list> 
</web-app> 
+0

什麼是您在本地使用的Web服務器,以及您做了哪些更改,以便ocalhost:8888/i重定向以更正jsp(除了web.xml中的更改)? – Santosh 2012-07-18 07:33:01

回答

1

記住AE是不完全一樣的:

By default, App Engine makes all files in the WAR available as static files except JSPs and files in WEB-INF/. Any request for a URL whose path matches a static file serves the file directly to the browser—even if the path also matches a servlet or filter mapping. You can configure which files App Engine treats as static files using the appengine-web.xml file.

https://developers.google.com/appengine/docs/java/gettingstarted/staticfiles

路線配置的AppEngine-web.xml中的位置https://developers.google.com/appengine/docs/java/config/appconfig

我會嘗試它聲明明確地說:

<static-files> 
    <include path="info.html" /> 
</static-files> 

如果這樣做不起作用只會將我的.html頁面更改爲真正的jsp頁面(即使沒有動態內容)。

如果沒有工作,想嘗試在一個servlet中一個RequestDispatcher轉發html頁面看到http://www.jguru.com/faq/view.jsp?EID=1310997

順便說一句,我的經驗是,DEVMODE服務器和部署服務器有時不同的解析文件,即使appengine-web.xml是相同的。

0

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.

也就是說,如果您想將.html映射到URL,那麼您必須確保它不是靜態的;因爲我們知道靜態文件服務於而不管servlet和過濾器映射如何

要做到這一點,你必須從靜態文件與排除:

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

這將使該.HTML文件只是一個資源文件(所有文件默認情況下,靜態和資源文件;除.jsp文件和web-inf文件夾中的文件)。

但不幸的是,這仍然不會使App Engine爲.html文件選取URL映射(使用'jsp-file'定義)。最可能的原因是servlet映射最終指向一個servlet,並且每個.jsp文件都會生成一個servlet,但對於.html文件(或任何其他文件)也是如此。

我可以想出的唯一解決方案是將.html文件重命名爲.jsp(並刪除靜態文件排除;您不再需要這些)。這樣你可以將它映射到任何URL。

有趣的是,您可以將非.jsp文件映射到開發服務器上的URL;只是與App Engine的另一個不一致。 ;)

相關問題