springboot - 在servlet中映射Errors 脱离spring mvc
应用不用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、执行
springboot - 在servlet中映射Errors 脱离spring mvc的更多相关文章
- Spring MVC(一)Servlet 2.x 规范在 Spring MVC 中的应用
Spring MVC(一)Servlet 2.x 规范在 Spring MVC 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/1019869 ...
- springboot注册到consul中报错:Spring MVC found on classpath, which is incompatible with Spring Cloud
今天在做springboot整合成springCloud并注册到consul中时,发现若注册到consule中成功 则不能启动swagger,且不能提供任何API服务,要是能提供API服务则不能注册到 ...
- 控制层技术:Servlet+reflection、Struts2、Spring MVC三者之间的比较学习
Servlet Struts2 Spring MVC 处理用户提交的数据 基于MVC设计模式的Web应用程序 是一个框架 是MVC框架 导入servlet包,配置web.xml文件 web.xml & ...
- servlet中如何实现通过Spring实现对象的注入
@WebServlet("/BaseServlet")public class BaseServlet extends HttpServlet { private static f ...
- Spring MVC URL的映射问题 ;Spring MVC 跳转与iframe包含地址问题
/login/login.html 进行form提交,登录之后的页面位于/main/frame.jsp; 这样的controller中的地址需要映射成/main/login.do,然后在control ...
- 将前端请求中的数据绑定到Spring MVC响应方法中参数的四种方法
一.映射URL绑定的占位符到方法参数 1.方法 使用@PathVariable注解 2.代码示例 a.接收请求方法 @RequestMapping(value = "/deleteInfo/ ...
- 最近无意中看到一个讲解spring mvc的系列,从源码的角度讲解,特记录下来,供以后反复学习
SpringMVC深度探险(一) —— SpringMVC前传 SpringMVC深度探险(二) —— SpringMVC概览 SpringMVC深度探险(三) —— DispatcherServle ...
- Spring MVC 实现文件的上传和下载
前些天一位江苏经贸的学弟跟我留言问了我这样一个问题:“用什么技术来实现一般网页上文件的上传和下载?是框架还是Java中的IO流”.我回复他说:“使用Spring MVC框架可以做到这一点,因为Spri ...
- springboot学习笔记:5.spring mvc(含FreeMarker+layui整合)
Spring Web MVC框架(通常简称为"Spring MVC")是一个富"模型,视图,控制器"的web框架. Spring MVC允许你创建特定的@Con ...
随机推荐
- Codeforces1307B. Cow and Friend
本题的难点是可以在y轴正轴动,但也是突破点,知道x轴都是整数,那么对于任意长度来说,能到达的最短是1,最长是本身长度,那么我们就选择最长的距离,跳到一个点,使这个点为再跳就超过终点,那么就可以用2次跳 ...
- Ubuntu16.04深度学习基本环境搭建,tensorflow , keras , pytorch , cuda
Ubuntu16.04深度学习基本环境搭建,tensorflow , keras , pytorch , cuda Ubuntu16.04安装 参考https://blog.csdn.net/flyy ...
- 一 SpringMvc概述&入门配置
SpringMVC: 类似Struts2的MVC框架,属于SpringFrameWork的后续产品. 与Struts2的区别: 参数传递: Struts2通过模型驱动,属性设置set方法,值栈.类级 ...
- C#文件过滤器filter---转载
C#文件过滤器filter OpenFileDialog对话框的Filter属性说明: 首先说明一个示例,分析一下Filter属性的构成:“ Excel文件|*.xls ”,前面的“Excel文件”成 ...
- Laradock 下安装Beast扩展
laradock/php-fpm/Dockfile ########################################################################## ...
- Vue - 引入本地图片的两种方式
第一种,只引入单个图片,这种引入方法在异步中引入则会报错. 比如需要遍历出很多图片展示时 <image :src = require('图片的路径') /> 第二种,可引入多个图片,也可引 ...
- P1091合唱队形(LIS问题)
题目描述(题目链接:https://www.luogu.org/problem/P1091) NN位同学站成一排,音乐老师要请其中的(N-KN−K)位同学出列,使得剩下的KK位同学排成合唱队形. 合唱 ...
- spring事务代码实践
事务一般是指数据库事务,是指作为一个程序执行单元执行的一系列操作,要么完全执行,要么完全不执行.事务就是判断以结果为导向的标准. 一.spring的特性(ACID) (1).原子性(atomicity ...
- IOS pin约束问题 存在间隙
今天在为自己的view添加约束 对比以前添加的约束时,发现有有两层淡红色线框一条实线和一条虚线,而以前一个demo中添加的则只有一个蓝色实线框. 今天添加的约束如图1所示: 图1 而以前添加约束如图2 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:显示和隐藏内容
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...