2011-02-11 52 views
3

我在Tomcat上運行基於Spring框架的Web應用程序。我的要求是,任何來到服務器的請求都應該到達一個servlet。所以我的conf/web.xml中作了如下配置HttpServletRequest的getRequestURL()不返回確切的URL

<error-page> 
    <error-code>404</error-code> 
    <location>/displayMsg.do</location> 
</error-page> 

因此,當用戶訪問http://myhost/xyz,request.getRequestURL()返回 http://myhost/displayMsg.do

我認爲對於requestURL的唯一原因()返回這個網址是因爲我所做的錯誤頁面配置。可能是因爲tomcat覆蓋這個url到這個字符串。

在這種情況下可以得到確切的URL嗎? 請幫忙

+3

的可能重複[JSP/Servlet的HTTP 404錯誤處理] (http://stackoverflow.com/questions/3336501/jsp-servlet-http-404-error-handling) – BalusC 2011-02-11 13:50:48

回答

1

幾周前我遇到了同樣的問題。

您可以用拉

request.getAttribute實際的URL(\ 「javax.servlet.forward.request_uri \」)。的toString()

+0

是的,這返回uri。 request.getRequestURL()給出了http:// localhost字符串。這兩個字符串的連接有所幫助。 謝謝。 – ihavprobs 2011-02-16 06:50:39