应用不用Spring MVC, 采用ErrorPageRegistrar 接口能直接映射errors。
1、概览
2、java代码
1)、MyAppServlet
package com.ebc.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet(name = "appServlet", urlPatterns = "/app/*") public class MyAppServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String requestURI = req.getRequestURI(); if (requestURI.endsWith("test")) { throw new RuntimeException("test exception from " + requestURI); } else if (requestURI.endsWith("test2")) { throw new ServletException("test servlet exception"); } else { resp.getWriter().println("Response from appServlet at " + requestURI); } } }
2)、MyErrorPageRegistrar
package com.ebc.servlet; import org.springframework.boot.web.server.ErrorPage; import org.springframework.boot.web.server.ErrorPageRegistrar; import org.springframework.boot.web.server.ErrorPageRegistry; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; @Component public class MyErrorPageRegistrar implements ErrorPageRegistrar { @Override public void registerErrorPages(ErrorPageRegistry registry) { registry.addErrorPages( createNotFoundErrorPage(), createRuntimeExceptionPage(), createOtherErrorPage()); } private ErrorPage createNotFoundErrorPage() { return new ErrorPage(HttpStatus.NOT_FOUND, "/notFoundServlet"); } private ErrorPage createRuntimeExceptionPage() { return new ErrorPage(RuntimeException.class, "/runtimeExceptionHandler"); } private ErrorPage createOtherErrorPage() { return new ErrorPage(Throwable.class, "/WEB-INF/internal-error.jsp"); } }
3)、NotFoundServlet
package com.ebc.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @WebServlet(name = "not-found-error-servlet", urlPatterns = "/notFoundServlet") public class NotFoundServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); PrintWriter writer = resp.getWriter(); writer.write("<h2>Page not found </h2>"); String requestUri = (String) req.getAttribute("javax.servlet.error.request_uri"); writer.write("request uri: " + requestUri); writer.write("<br>Response from " + this.getClass()); } }
4)、RuntimeExceptionServlet
package com.ebc.servlet; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @WebServlet(name = "runtime-handler-servlet", urlPatterns = "/runtimeExceptionHandler") public class RuntimeExceptionServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); PrintWriter writer = resp.getWriter(); writer.write("<h2>RuntimeException</h2>"); String requestUri = (String) req.getAttribute(RequestDispatcher.ERROR_REQUEST_URI); writer.write("request uri: " + requestUri); Integer code = (Integer) req.getAttribute(RequestDispatcher.ERROR_STATUS_CODE); writer.write("<br>status code: " + code); String errorMessage = (String) req.getAttribute(RequestDispatcher.ERROR_MESSAGE); writer.write("<br>error message: " + errorMessage); writer.write("<br>Response from "+this.getClass()); } }
5)、MyApplication
package com.ebc; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @ServletComponentScan public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
3、internal-error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h3>Error on server side</h3> <p> <%= request.getAttribute("javax.servlet.error.exception") %> </p> <p>From jsp page.</p> </body> </html>
4、pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
5、执行