Java 当servlet发生异常时如何重定向到错误页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26801038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to redirect to error page when exception occurs from servlet?
提问by Raghu
I am writing a servlet, in that if any exception occurs i donэt want to display exception/error message on browser, so I will redirect to my customized error page. So I have done like this:
我正在编写一个 servlet,因为如果发生任何异常,我不想在浏览器上显示异常/错误消息,所以我将重定向到我自定义的错误页面。所以我这样做了:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try{
//Here is all code stuff
}catch(Exception e){
request.getRequestDispatcher("/ErrorPage.jsp").forward(request, response);
e1.printStackTrace();
}
Is this the correct way, if I am wrong please correct me and if there is any better mechanism please tell me.
这是正确的方法,如果我错了请纠正我,如果有更好的机制请告诉我。
采纳答案by Roman C
Only way to handle it in a generic way is to use web.xml
like below:
以通用方式处理它的唯一方法是使用web.xml
如下:
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/ErrorHandler</location>
</error-page>
The servlet is thrown ServletException
and IOException
but if you want to handle runtime exceptions and all other exceptions in a single exception handler, you can provide exception-type as Throwable
. You can use multiple error-page entries that will handle different type of exceptions and have different handlers.
servlet 被抛出ServletException
,IOException
但如果您想在单个异常处理程序中处理运行时异常和所有其他异常,您可以将异常类型提供为Throwable
. 您可以使用多个错误页面条目来处理不同类型的异常并具有不同的处理程序。
Example:
例子:
@WebServlet("/ErrorHandler")
public class ErrorHandler extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processError(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processError(request, response);
}
private void processError(HttpServletRequest request,
HttpServletResponse response) throws IOException {
//customize error message
Throwable throwable = (Throwable) request
.getAttribute("javax.servlet.error.exception");
Integer statusCode = (Integer) request
.getAttribute("javax.servlet.error.status_code");
String servletName = (String) request
.getAttribute("javax.servlet.error.servlet_name");
if (servletName == null) {
servletName = "Unknown";
}
String requestUri = (String) request
.getAttribute("javax.servlet.error.request_uri");
if (requestUri == null) {
requestUri = "Unknown";
}
request.setAttribute("error", "Servlet " + servletName +
" has thrown an exception " + throwable.getClass().getName() +
" : " + throwable.getMessage());
request.getRequestDispatcher("/ErrorPage.jsp").forward(request, response);
}
}
回答by aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
In some method, you would have the following:
在某些方法中,您将拥有以下内容:
try {
// something
} catch (Exception e) {
sendErrorRedirect(req, res, "/errorpage.jsp", e);
}
// then.... sendErrorRedirect looks like this:
protected void sendErrorRedirect(HttpServletRequest request, HttpServletResponse response, String errorPageURL, Throwable e) {
try {
request.setAttribute ("javax.servlet.jsp.jspException", e);
getServletConfig().getServletContext().getRequestDispatcher(errorPageURL).forward(request, response);
} catch (Exception ex) {
putError("serXXXXX.sendErrorRedirect ", ex);
}
}
回答by SMA
One way to handle it in a generic way is to use web.xml like below:
以通用方式处理它的一种方法是使用 web.xml,如下所示:
<error-page>
<exception-type>java.io.IOException</exception-type >
<location>/ErrorHandler</location>
</error-page>
I have just included IO exception but you might have say SQLException you could very well add another error-page and another location for the same. Similarly you could say java.lang.Exception type and one handler handling everything.
我刚刚包含了 IO 异常,但您可能会说 SQLException 您可以很好地添加另一个错误页面和另一个位置。同样,您可以说 java.lang.Exception 类型和一个处理程序处理一切。