2010-03-24 84 views
6

UPDATE:寫檢查,看看是否JSP的存在,並轉發到另一個JSP,如果他們是一個Servlet不

要澄清一個一般性錯誤捕手,捕捉404的沒有足夠的粒度對我來說。只有當jsp位於特定的目錄中,並且僅當文件名包含某個字符串時,我才需要執行此操作。

/UPDATE

我一直在負責編寫攔截在一個特定的目錄和JSP調用一個servlet,檢查文件是否存在,如果它不只是轉發該文件,如果它那麼我不會轉發到默認的JSP。 我設置了web.xml如下:

<servlet> 
<description>This is the description of my J2EE component</description> 
<display-name>This is the display name of my J2EE component</display-name> 
<servlet-name>CustomJSPListener</servlet-name> 
<servlet-class> ... CustomJSPListener</servlet-class> 
<load-on-startup>1</load-on-startup> 
</servlet> 
... 
<servlet-mapping> 
    <servlet-name>CustomJSPListener</servlet-name> 
    <url-pattern>/custom/*</url-pattern> 
</servlet-mapping> 

並且servlet的doGet方法如下:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    logger.debug(String.format("Intercepted a request for an item in the custom directory [%s]",request.getRequestURL().toString())); 
    String requestUri = request.getRequestURI(); 
      // Check that the file name contains a text string 
    if (requestUri.toLowerCase(Locale.UK).contains("someText")){ 
    logger.debug(String.format("We are interested in this file [%s]",requestUri)); 
    File file = new File(requestUri); 
    boolean fileExists = file.exists(); 
    logger.debug(String.format("Checking to see if file [%s] exists [%s].",requestUri,fileExists)); 
        // if the file exists just forward it to the file 
    if (fileExists){ 
    getServletConfig().getServletContext().getRequestDispatcher(
      requestUri).forward(request,response); 
    } else { 
        // Otherwise redirect to default.jsp 
    getServletConfig().getServletContext().getRequestDispatcher(
      "/custom/default.jsp").forward(request,response); 
    } 
    } else { 
        // We aren't responsible for checking this file exists just pass it on to the requeseted jsp 
    getServletConfig().getServletContext().getRequestDispatcher(
      requestUri).forward(request,response); 
    } 
} 

這似乎從Tomcat導致錯誤500,我想這是因爲servlet重定向到同一個文件夾,然後被servlet再次攔截,導致無限循環。 有沒有更好的方法來做到這一點?我導致我相信我可以使用過濾器來做到這一點,但我對它們不甚瞭解。

回答

8
File file = new File(requestUri); 

這是不對的。 java.io.File知道什麼都沒有關於它正在運行的webapp上下文。文件路徑將相對於當前工作目錄,這取決於你如何啓動應用服務器。例如,它可能與C:/Tomcat/bin相關,而不是您所期望的webapp根目錄。你不想擁有這個。

使用ServletContext#getRealPath()將相對網絡路徑轉換爲絕對磁盤文件系統路徑。 ServletContext在繼承的getServletContext()方法的servlet中可用。因此,下面要指出正確的文件:

String absoluteFilePath = getServletContext().getRealPath(requestUri); 
File file = new File(absoluteFilePath); 

if (new File(absoluteFilePath).exists()) { 
    // ... 
} 

或者,如果目標容器不擴大物理磁盤文件系統的戰爭,但在替代內存,那麼你最好使用ServletContext#getResource()

URL url = getServletContext().getResource(requestUri); 

if (url != null) { 
    // ... 
} 
+0

謝謝,我會試試這個。 – 2010-03-24 11:40:24

+0

這仍然無法阻止它進入無限循環的問題。 – 2010-03-24 11:45:35

+1

將' REQUEST'添加到''。順便說一句:過濾器確實是一個更好的地方,你可以在'doFilter()'中放入相同的代碼,你只需要向下施放請求和響應。 – BalusC 2010-03-24 12:17:02

3

這可以在更容易和內置的方式完成。

web.xml有<error-page>元素。你可以這樣做:

<error-page> 
    <error-code>404</error-code> 
    <location>/pageNotFound.jsp</location> 
<error-page> 
+0

我只希望發生這種情況,如果它在一個特定的目錄中,並且文件名包含某個字符串。所以抓住所有的404對我來說沒有足夠的粒度。 – 2010-03-24 11:06:08

+0

然後看到BalusC的迴應。 – Bozho 2010-03-24 11:29:56

0

我會做的是爲初始servlet請求創建一個虛擬目錄,然後轉發到真實目錄或404類型頁面。無論你在發佈一個前鋒,爲什麼不直接重定向到不同的目錄呢?這將避免任何循環問題。

+0

我想到了這一點,但我無法做到這一點,不幸的是它需要與JSP相同的目錄。 – 2010-03-24 12:07:40

相關問題