2012-09-15 92 views
0

我不希望使用每次:有沒有簡單的方法來處理JSP文件中的404錯誤?

if(!new File(file).exists()){ 
    //createFile /doc/a.txt ... 
} 
request.getRequestDispatcher("/doc/a.txt").forward(request, response); 

我想知道我怎樣才能實現下面的代碼:

try{ 
    request.getRequestDispatcher("/doc/a.txt").forward(request, response); 
}catch(404 error){ 
    //createFile /doc/a.txt ... 
} 

我怎麼能交給404錯誤只是在這個網頁?

非常感謝您的任何建議。

回答

0

這是最好的一個錯誤部分在web.xml處理:

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

感謝的答案,但我只需要發放404錯誤只是在此頁面中,不是全部 – Koerr

+0

是的。這是一個完美的解決方案。無需在try catch中處理Exception。只要提到應用程序遇到HTTP 404錯誤時顯示的錯誤頁面名稱即可。 –

+0

你是對的@ Reimeus,我覺得這有點複雜,我正在尋找一個更簡單的方法,如果可能 – Koerr

0

爲什麼沒有這個工作對你:

try{ 
    request.getRequestDispatcher("/doc/a.txt").forward(request, response); 
}catch(Exception error){ 
    //createFile /doc/a.txt ... 
} 
+0

我試過,'404 Not Found'錯誤不要拋出任何異常 – Koerr

相關問題