Reputation: 335
Is there way to include error page for any wrong path request? I need all wrong path requested by the user to be sent to error.jsp
For example if "/example" is part of the web.xml then it would send to localhost:8080/example If user specifies localhost:8080/examp then it would redirect to error.jsp
I tried the following code in the web.xml and created a error.jsp inside Web Pages directory in Netbeans and it still sent me to yahoo error handle page:
<web-app>
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.jsp</location>
</error-page>
</web-app>
error.jsp
<%@ page isErrorPage="true" %>
<html>
<head>
<title>Show Error Page</title>
</head>
<body>
<h1>Opps...</h1>
<p>An error occurred.</p>
</body>
</html>
Upvotes: 2
Views: 4878
Reputation: 778
First create a custom servlet and use it for both exceptions and errors. Refer the below code.
@WebServlet("/AppExceptionHandler")
public class AppExceptionHandler 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 {
// Analyze the servlet exception
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";
}
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.write("<html><head><title>Exception/Error Details</title></head><body>");
if(statusCode != 500){
out.write("<h3>Error Details</h3>");
out.write("<strong>Status Code</strong>:"+statusCode+"<br>");
out.write("<strong>Requested URI</strong>:"+requestUri);
}else{
out.write("<h3>Exception Details</h3>");
out.write("<ul><li>Servlet Name:"+servletName+"</li>");
out.write("<li>Exception Name:"+throwable.getClass().getName()+"</li>");
out.write("<li>Requested URI:"+requestUri+"</li>");
out.write("<li>Exception Message:"+throwable.getMessage()+"</li>");
out.write("</ul>");
}
out.write("<br><br>");
out.write("<a href=\"index.html\">Home Page</a>");
out.write("</body></html>");
}
}
Then configure it in deployment descriptor which is your web.xml
<welcome-file-list>
<welcome-file> index.html</welcome-file >
</welcome-file-list>
<error-page>
<error-code> 404</error-code>
<location> /AppExceptionHandler</location >
</error-page>
<error-page>
<exception-type> javax.servlet.ServletException</exception-type>
<location>/AppExceptionHandler</location >
</error-page>
<error-page>
<exception-type >java.lang.Throwable </exception-type>
<location >/AppExceptionHandler </location>
when a exception occures it will call the AppExceptionHandler class and then it will cause to display a custom error page.
Upvotes: 2