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 ...
随机推荐
- 第2节 Scala中面向对象编程:12、13、14、15、16、trait
6.4. Scala中面向对象编程之trait 6.4.1. 将trait作为接口使用 Scala中的trait是一种特殊的概念: 首先先将trait作为接口使用,此时的trait就与Java ...
- Thymeleaf的内置属性(转)
原文链接: http://somefuture.iteye.com/blog/2253761 Thymeleaf是另一个Java视图模板引擎,使用上和FreeMarker各有千秋,不了解的可以从其他博 ...
- 使用eclipse创建一个简单的Java Web应用程序
关于Java JDK/JRE.Tomcat的配置等等都没什么好说的,主要记录一下使用Eclipse创建web工程时的一些点以及说一说自己用IDEA的创建失败的过程(IDEA没运行成功...暂时不想弄了 ...
- Python 基础之生成器
一.生成器表达式 生成器本质是迭代器,允许自定义逻辑的迭代器迭代器和生成器区别:迭代器本身是系统内置的,重写不了.而生成器是用户自定义的,可以重写迭代逻辑生成器可以用来钟方式创建: (1)生成器 ...
- Spring MVC中的ResponseEntity和ResponseBody的区别
1.ResponseEntity的优先级高于@ResponseBody. 在不是ResponseEntity的情况下才去检查有没有@ResponseBody注解. 如果响应类型是ResponseEnt ...
- python神器-pycharm安装与使用(全网最新)
pycharm分为社区版(免费)和专业版(收费/破解),以下安装仅适用于社区版 下载方式1: 官网下载:https://www.jetbrains.com/pycharm/download/dow ...
- DoMes平台首页菜单栏
问题1:左侧菜单栏数据是在哪里获取的? 答案1: 项目根目录的Views/Home/Index文件为平台首页 打开Index.cshtml文件,有一个framework-clientdata.js引入 ...
- Pandas 用法汇总
一.生成数据表 1.首先导入pandas 库,一般会用到 numpy 库,所以我们先导入备用: import numpy as np import pandas as pd 2.生成 CSV 或者 x ...
- 攻防世界web进阶区(1)
1.题目地址:http://111.198.29.45:43589 页面提示打开robots文件,则: 页面有提示输入fl0g.php,那么 获取flag. 2.题目地址:http://111.198 ...
- C#中SqlDataAdapter的使用小结---转载
C#中SqlDataAdapter的使用小结 转载 叁木-Neil 最后发布于2018-06-07 21:29:39 阅读数 8275 收藏 展开 SqlDataAdapter对象 一.特点介绍1.表 ...