- ステータスコードが40Xの場合はエラーコードを出力(error4XX.jsp)
- ステータスコードがそれ以外の場合はエラーコードとエラースタックを出力(error50X.jsp)
error4XX.jsp
<%
Object codeObj = (Object)request.getAttribute("javax.servlet.error.status_code");
int code = 0;
if(codeObj != null){
code = ((Integer)codeObj).intValue();
}
String errorMessage = "";
if(code == 400){
errorMessage = code + ":Bad Request";
}
if(code == 401){
errorMessage = code + ":Unauthorized";
}
if(code == 403){
errorMessage = code + ":Forbidden";
}
if(code == 404){
errorMessage = code + ":Not Found";
}
if(code == 408){
errorMessage = code + ":Request Time-out";
}
%>
<h1><%= errorMessage %></h1>
//ここから下に空白を512バイトいれておく(IE対策)
error50X.jsp
<%
String errorMessage = "";
StackTraceElement[] es = new StackTraceElement[]{};
errorMessage = "アプリケーションエラーが発生しました。";
if(exception == null){
//ない場合はrequestから取得してみる
exception = (Throwable)request.getAttribute("javax.servlet.error.exception");
}
if(exception != null){
es = exception.getStackTrace();
}
%>
<h1><%= errorMessage %></h1>
<%
//ここは最終的にログ出力にするなどして見せないように
if(exception != null){
out.println(exception.getMessage() + "<br>");
}
for(int i = 0; i < es.length; i++){
out.println(es[i] + "<br>");
}
%>
//ここから下に空白を512バイトいれておく(IE対策)
web.xml(JSPのディレクティブであるerrorPageは使わない)
<error-page>
<error-code>400</error-code>
<location>/error4XX.jsp</location>
</error-page>
<error-page>
<error-code>401</error-code>
<location>/error4XX.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/error4XX.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error4XX.jsp</location>
</error-page>
<error-page>
<error-code>408</error-code>
<location>/error4XX.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error50X.jsp</location>
</error-page>