如何处理转发到具有不同消息的同一错误页面的servlet中的异常? 在我的servlet中,我有以下doGet(这里简化)方法,带有一个switch case:
package com.mycompany.test_servlet;
import javax.servlet.ServletException;
public class MyException extends ServletException {
private String message = null;
public MyException() {
super();
}
public MyException(String message) {
super(message);
}
public MyException(Throwable cause) {
super(cause);
}
}
然后我有自定义异常:
<error-page>
<location>/Error.jsp</location>
</error-page>
和web.xml文件:
{{1}}
答案 0 :(得分:0)
尝试指出异常类型:
<error-page>
<exception-type>com.mycompany.test_servlet.MyException</exception-type>
<location>/Error.jsp</location>
</error-page>
答案 1 :(得分:0)
您可以尝试在请求对象中设置属性,同时捕获异常
request.setAttribute("exception", ex.getMessage());
然后在error.jsp中获取此属性,例如:
<% if (request.getAttribute("exception") != null) { %>
<tr>
<td colspan='2'><p align=right>Authentication Error (try again):<br>
<%=request.getAttribute("exception")%>
</p></td>
</tr>
<%}%>