应用不用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的更多相关文章

  1. Spring MVC(一)Servlet 2.x 规范在 Spring MVC 中的应用

    Spring MVC(一)Servlet 2.x 规范在 Spring MVC 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/1019869 ...

  2. springboot注册到consul中报错:Spring MVC found on classpath, which is incompatible with Spring Cloud

    今天在做springboot整合成springCloud并注册到consul中时,发现若注册到consule中成功 则不能启动swagger,且不能提供任何API服务,要是能提供API服务则不能注册到 ...

  3. 控制层技术:Servlet+reflection、Struts2、Spring MVC三者之间的比较学习

    Servlet Struts2 Spring MVC 处理用户提交的数据 基于MVC设计模式的Web应用程序 是一个框架 是MVC框架 导入servlet包,配置web.xml文件 web.xml & ...

  4. servlet中如何实现通过Spring实现对象的注入

    @WebServlet("/BaseServlet")public class BaseServlet extends HttpServlet { private static f ...

  5. Spring MVC URL的映射问题 ;Spring MVC 跳转与iframe包含地址问题

    /login/login.html 进行form提交,登录之后的页面位于/main/frame.jsp; 这样的controller中的地址需要映射成/main/login.do,然后在control ...

  6. 将前端请求中的数据绑定到Spring MVC响应方法中参数的四种方法

    一.映射URL绑定的占位符到方法参数 1.方法 使用@PathVariable注解 2.代码示例 a.接收请求方法 @RequestMapping(value = "/deleteInfo/ ...

  7. 最近无意中看到一个讲解spring mvc的系列,从源码的角度讲解,特记录下来,供以后反复学习

    SpringMVC深度探险(一) —— SpringMVC前传 SpringMVC深度探险(二) —— SpringMVC概览 SpringMVC深度探险(三) —— DispatcherServle ...

  8. Spring MVC 实现文件的上传和下载

    前些天一位江苏经贸的学弟跟我留言问了我这样一个问题:“用什么技术来实现一般网页上文件的上传和下载?是框架还是Java中的IO流”.我回复他说:“使用Spring MVC框架可以做到这一点,因为Spri ...

  9. springboot学习笔记:5.spring mvc(含FreeMarker+layui整合)

    Spring Web MVC框架(通常简称为"Spring MVC")是一个富"模型,视图,控制器"的web框架. Spring MVC允许你创建特定的@Con ...

随机推荐

  1. 微擎系统BUG漏洞解决方法汇总

    微擎微赞系统BUG漏洞解决方法汇总 弄了微擎系统来玩玩,发觉这个系统BUG还不少,阿里云的提醒都一大堆,主要是没有针对SQL注入做预防,处理的办法基本都是用转义函数. 汇总: 1. 漏洞名称: 微擎任 ...

  2. Django 学习之Xadmin

    一.xadmin的特点 1.基于Bootstrap3:Xadmin使用Bootstrap3.0框架精心打造.基于Bootstrap3,Xadmin天生就支持在多种屏幕上无缝浏览,并完全支持Bootst ...

  3. JavaScript图形实例:正多边形

    圆心位于坐标原点,半径为R的圆的参数方程为 X=R*COS(θ) Y=R*SIN(θ) 在圆上取N个等分点,将这N个点首尾连接N条边,可以得到一个正N边形. 1.正多边形阵列 构造一个8行8列的正N( ...

  4. Java程序生成exe可执行文件

    Java程序打包成exe可执行文件,分为两大步骤. 第一步:将Java程序通过Eclipse或者Myeclipse导成Jar包 第二步:通过exe4j讲Jar包程序生成exe可执行文件 第一步详解: ...

  5. 通过python 构建一个简单的聊天服务器

    构建一个 Python 聊天服务器 一个简单的聊天服务器 现在您已经了解了 Python 中基本的网络 API:接下来可以在一个简单的应用程序中应用这些知识了.在本节中,将构建一个简单的聊天服务器.使 ...

  6. 虚拟机下安装 VMwareTools 实现宿主机和虚拟机的文件共享

    $ mount /dev/sr0 /media/ #点击 虚拟机 安装 VMwareTools 挂载 $ cd /media/ $ cp VMwareTools-10.1.6-5214329.tar. ...

  7. Django:邮件功能实现

    django-users2和django的邮件功能模块都有相关的实现 ----------------------------------------------------------------- ...

  8. maven更新JRE更改JSE1.5

    1. [代码]在maven的配置文件settings.xml中的<profiles>标签里添加如下代码,设置默认JRE编译版本为1.7 <profile> <id> ...

  9. django静态文件处理

    django静态文件处理   从开始接接触python这门语言已有四年了,中间陆续的学习,又不断的忘记,所以基本上是没有系统的知识体系.但是挺喜欢这门简洁,强大的动态语言.最近自己私人有个项目要做,虽 ...

  10. acid-事务的原子性、一致性、隔离性、持久性

    博客分类: oracle-dba   原子性  多个事情组成一个单元,要么同时成功或失败,不能只运行其中一个 一致性  事务处理要将数据库从一种状态转变为另一种状态. 一旦提交了修改数据,那么其它人读 ...