在web.xml中,可以像這樣指定一個錯誤頁面。
讓我們假設你想趕上HTTP400,500種異常:
<error-page>
<error-code>400</error-code>
<location>/errorpage.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/errorpage.html</location>
</error-page>
(由Arjit的建議)
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/errorpage.html</location>
</error-page>
然後把它放在一起,由DeveloperWJK的建議,在servlet :
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, NullPointerException
{
String param = request.getParameter("param");
if(param.equals("x"))
{
response.sendRedirect("x.jsp");
return;
}
}
閱讀有關servlet異常處理程序以及如何將異常映射到這些處理程序的信息。 –
從我讀過的內容來看,通常的做法是讓servlet拋出某種異常,並允許容器處理它。問題是,如果我不能檢查null,或者使用try/catch,我不知道是否/何時拋出異常。 – user1154644
只需將該異常添加到處理請求的方法的簽名即可。 –