我尝试使用自定义错误页面重定向 400 错误,但它不起作用。 400 只是不重定向,404 和 500 工作正常
1:
<error-page>
<error-code>400</error-code>
<location>/faces/errors.xhtml</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/faces/notfound.xhtml</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/faces/error.xhtml</location>
</error-page>
2: 在服务器级别更改 --sever.xml 文件也不起作用
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.ErrorReportValve"
errorCode.400="/faces/errors.xhtml"
showReport="false"
showServerInfo="false" />
3 创建了servlet类来重定向错误,但它也不起作用
package tneb.ccms.consumer;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/error400")
public class Error400Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Set response content type
//System.out.println( request.getRequestURI());
response.setContentType("text/html");
System.out.println("error page");
// Set response status to 400 Bad Request
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.sendRedirect(request.getContextPath()+"/faces/errors.xhtml");
}
}
package tneb.ccms.consumer;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class BadRequestFilter implements Filter {
private FilterConfig filterConfig;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
try {
chain.doFilter(request, response);
} catch (Exception e) {
if (httpResponse.getStatus() == HttpServletResponse.SC_BAD_REQUEST) {
httpRequest.getRequestDispatcher("/faces/errors.xhtml").forward(request, response);
} else {
throw e;
}
}
}
@Override
public void destroy() {
// Cleanup code if needed
}
}
我尽了一切努力并参考文档仍然无法解决这个问题
hello-tomcat9-web
├── pom.xml
└── src
└── main
├── java
│ └── com
│ └── example
│ └── HelloStatusServlet.java
└── webapp
├── 400.html
├── 404.html
├── 500.html
├── index.jsp
└── WEB-INF
└── web.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>helloweb</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>helloweb</name>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>helloworld</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
只需在web.xml中设置相应的网页 - 状态码400、404、500即可。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>hello-tomcat9-web</display-name>
<error-page>
<error-code>400</error-code>
<location>/400.html</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.html</location>
</error-page>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
</welcome-file-list>
</web-app>
HelloStatusServlet可用于模拟生成并返回400和500状态码。
package com.example;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/helloStatus")
public class HelloStatusServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String errorType = request.getParameter("error");
if ("400".equals(errorType)) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Simulated 400 Error");
return;
} else if ("500".equals(errorType)) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Simulated 500 Error");
return;
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>Hello, World!</h1><p/>");
out.println("<h1>Status TEST!</h1><p/>");
out.println("</body></html>");
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Hello Page</title>
</head>
<body>
<h1>Hello</h1>
<p>Current Date and Time: <%= new java.util.Date() %></p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Bad Request</title>
</head>
<body>
<h1>400 - Bad Request</h1>
<p>ZZZZZ - The request could not be understood by the server due to malformed syntax.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Page Not Found</title>
</head>
<body>
<h1>404 - Page Not Found</h1>
<p>ZZZZZ - The requested resource could not be found on this server.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Internal Server Error</title>
</head>
<body>
<h1>500 - Internal Server Error</h1>
<p>ZZZZZ - Sorry, something went wrong on our end. Please try again later.</p>
</body>
</html>
mvn clean package
将
helloworld.war
放入Tomcat/webapps/
返回index.jsp
返回 helloStatus servlet。
模拟状态代码 400。
模拟状态代码 500。
返回404