tomcat自定义400错误页面

article/2025/10/4 5:35:24

原文链接:https://aspiresoftware.in/blog/catalinatomcat-custom-error-report-valve-to-handle-errors-exceptions/

 

<Valve className="org.apache.catalina.valves.ErrorReportValve" showReport="false" showServerInfo="false" />

 

通过在server.xml文件中 增加上面代码 重启tomcat 400页面如下

 

自定义 400 错误页面方法

1.添加依赖

<dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-catalina</artifactId><version>${tomcat.version}</version>
</dependency>
<dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-coyote</artifactId><version>${tomcat.version}</version>
</dependency>
<dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-el-api</artifactId><version>${tomcat.version}</version>
</dependency>
<dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-jaspic-api</artifactId><version>${tomcat.version}</version>
</dependency>
<dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-juli</artifactId><version>${tomcat.version}</version>
</dependency>
<dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-servlet-api</artifactId><version>${tomcat.version}</version>
</dependency>
<dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-util</artifactId><version>${tomcat.version}</version>
</dependency>
<dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-util-scan</artifactId><version>${tomcat.version}</version>
</dependency>

2 创建类并继承 ErrorReportValve

public class CustomErrorReportValve extends ErrorReportValve

实现方法

@Override protected void report(Request request, Response response, Throwable t)

if (response.getStatus() == 400) {StringBuilder sb = new StringBuilder(); //Give relative path of htmlString pageString = getHtmlPage("./400.html");response.setContentType("text/html");response.setCharacterEncoding("utf-8");sb.append(pageString);Writer writer = response.getReporter();if (writer != null) {writer.write(sb.toString());}
}

public String getHtmlPage(String path) throws IOException {File file = new File("test");file.exists();StringBuilder sb = new StringBuilder();BufferedReader br = new BufferedReader(new FileReader(path));try {String line = br.readLine();while (line != null) {sb.append(line);sb.append(System.getProperty("line.separator"));line = br.readLine();}} finally {br.close();}return sb.toString();}


http://chatgpt.dhexx.cn/article/E2kex0Hr.shtml

相关文章

java 400_Java项目报400错误的原因与解决方法

java项目中400错误介绍&#xff1a;(推荐&#xff1a;java视频教程) 400 Bad Request&#xff1a; 请求中的语法错误。Reason-Phrase应当标志这个详细的语法错误&#xff0c;比如”Missing Call-ID header field”。 HTTP 400 错误 - 请求无效 (Bad request) 在ajax请求后台数据…

uniapp:request 请求出现400错误

uniapp&#xff1a;request 请求出现400错误 开发需求&#xff1a;我们团队在使用 uniapp springboot 开发微信小程序项目&#xff0c;前端向后端发送request 请求时&#xff0c;前端爆出400错误。 400错误&#xff1a; 请求无效 (Bad request);出现这个请求无效报错说明请求没…

服务器返回400错误

运维的项目&#xff0c;莫名其妙的有些接口报了400错误&#xff0c;和前端一起检查了传参格式、传参实体类是否一一对应等&#xff0c;因为入参有中文&#xff0c;顺带把编码格式也查了&#xff0c;但都没问题&#xff0c;而且最神奇的是&#xff0c;正式服该接口就报400错误&a…

JAVA400错误_程序访问一个地址时候报400错误,浏览器访问正常怎么解决

最近凯哥在调程序的时候&#xff0c;发现以前好好的程序&#xff0c;突然不能用了。于是就本地断点&#xff0c;发现了如下错误&#xff1a; 程序通过httpClient访问一个地址的时候&#xff0c;提示&#xff1a;server returned HTTP Response code &#xff1a;400 fro URL:XX…

tomcat 400错误

本片文章是对tomcat 400 错误造成信息泄露&#xff0c;如何进行修复进行描述&#xff0c;希望可以对非安全专业的互联网工作人员提供一点帮助&#xff0c;本片文章内容确实也是笔者最近遇到的一些问题。 漏洞描述 tomcat 发生400 错误&#xff0c;暴露出中间件版本、以及程序…

html400错误,http400错误如何解决

还有不少小伙伴们都碰到了http400错误如何解决这个疑惑吧&#xff0c;那么究竟该怎样解决呢&#xff1f;不妨就让iefans网小编告诉大家处理http400错误方法讲解&#xff0c;希望能够帮助到各位解决这个问题。 方法/步骤分享&#xff1a; 一、前端ajax请求&#xff0c;修改需要使…

接口400错误解析

今天我遇上一个让我很痛心的错误400。对程序员来说&#xff0c;这可能是一个最简单的错误码。因为这个相应并没有进拦截器&#xff0c;更没有进到Controller层。可我在解决这个问题时&#xff0c;质询了很多朋友不解&#xff0c;百度各种方案无效。苦苦耗费我大半天时间。 首先…

400错误请求怎么解决_什么是400错误请求错误(以及如何解决)?

400错误请求怎么解决 A 400 Bad Request Error occurs when a request sent to the website server is incorrect or corrupt, and the server receiving the request can’t understand it. Occasionally, the problem is on the website itself, and there’s not much you c…

HTTP 400错误

HTTP 400错误&#xff1a; 400 是 HTTP 的状态码&#xff0c;主要有两种形式&#xff1a; 1、bad request 意思是 “错误的请求”&#xff1b;2、invalid hostname 意思是 “不存在的域名”。 400 Bad Request 是由于明显的客户端错误&#xff08;例如&#xff0c;格式错误的…

rapidjson安装学习

这里主要记录几个要点&#xff0c;后面来补充吧&#xff0c;很晚了 源码是鹅厂大佬写的&#xff0c;佩服佩服~ 一、RapidJSON介绍及资料 RapidJSON是腾讯开源的C JSON解析及生成器&#xff0c;只有头文件的C库&#xff0c;跨平台。 RapidJSON 是一个 C 的 JSON 解析器及生成器…

rapidjson安装使用

前言&#xff1a;仅个人小记。 正文 由于 rapidjson是 “header-only”的C库&#xff0c;故而直接将头文件目录拷贝到系统目录或者指定目录即可完成安装。 参考材料&#xff1a; rapidjson代码仓库 https://github.com/Tencent/rapidjson rapidjson 文档 https://rapidjson.…

rapidjson创建json字符串

参考链接&#xff1a;http://rapidjson.org/zh-cn/ #include "json/stringbuffer.h" #include "json/prettywriter.h"void getJson() {rapidjson::StringBuffer buf;rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buf);writer.StartObj…

RapidJson踩坑记录

用于记录RapidJson使用中的坑位&#xff0c;持续更新。关于rapidjson的详细说明&#xff0c;可以参加参考文档&#xff1a;http://rapidjson.org/zh-cn/md_doc_tutorial_8zh-cn.html#CreateString 1、添加字符串元素 现象&#xff1a; #include "rapidjson/document.h&…

c++ rapidjson

下面rapid json代码已在vs2017验证&#xff0c;特别地&#xff0c;用rapid json可以解析中文字符串&#xff0c;不会中文乱码。 第一步&#xff1a;去https://github.com/Tencent/rapidjson/上下载头文件&#xff0c;只需要其中的include文件夹。也可以在csdn上下载&#xff1…

RapidJSON简介及使用

RapidJSON是腾讯开源的一个高效的C JSON解析器及生成器&#xff0c;它是只有头文件的C库。RapidJSON是跨平台的&#xff0c;支持Windows, Linux, Mac OS X及iOS, Android。它的源码在GitHub - Tencent/rapidjson: A fast JSON parser/generator for C with both SAX/DOM style …

JSON--rapidjson介绍

JSON--rapidjson 1 RapidJSON简介2 C/C Json库对比一致性解析时间解析内存Stringify Time&#xff08;string 2 json&#xff09;Prettify Time&#xff08;美化格式时间&#xff09;代码大小 3 几个重点库介绍rapidjsonnlohmann-jsonjsoncppcjson 参考 1 RapidJSON简介 Rapid…

RapidJSON入门:手把手教入门实例介绍

RapidJSON优点 跨平台 编译器&#xff1a;Visual Studio、gcc、clang 等 架构&#xff1a;x86、x64、ARM 等 操作系统&#xff1a;Windows、Mac OS X、Linux、iOS、Android 等 容易安装 只有头文件的库。只需把头文件复制至你的项目中。 独立、最小依赖 不需依赖 STL、BOOST …

oracle listagg如何去重

listagg去重 去重思路&#xff1a;利用listagg会忽略null值的特点 按ENTITY_GROUP_RRN 分组&#xff0c;用 listagg 分别合并 EQPT_ID 与 STATION_ID &#xff0c;同时要求去重 表 T_TEST 数据如下&#xff1a; EQPT_IDENTITY_GROUP_RRNSTATION_IDTOOL-00110493721JITAI-1TO…

mysql listagg within_Oracle的 listagg() WITHIN GROUP ()函数使用

1.使用条件查询 查询部门为20的员工列表 -- 查询部门为20的员工列表 SELECT t.DEPTNO,t.ENAME FROM SCOTT.EMP t where t.DEPTNO 20 ; 效果&#xff1a; 2.使用 listagg() WITHIN GROUP () 将多行合并成一行 SELECT T .DEPTNO, listagg (T .ENAME, ,) WITHIN GROUP (ORDER …

listagg结果去重

最近在一个项目中用到了listagg方法&#xff0c;但是在组合结果中出现有重复的情况。默认的结果如下 于是我就写了一个方法对listagg的结果去重&#xff0c;也可以对该格式的字符串去重&#xff0c;方法如下 create or replace function listaggpure(targetStr varchar2,seper…