403 错误页面在 Tomcat 7 上的 运行 我的 Web 服务中不起作用

403 error page is not working in my web service which is running on a Tomcat 7

我正在使用 Jersey 开发 Rest Web 服务。该服务是 Tomcat 上的 运行 7. 在我的 "WebContent" 文件夹下我放了三个 xml 文件。一个名为 error403.xml,另一个名为 error404.xml,另一个名为 error505.xml。同样在我的 web.xml 中,我有以下几行:

    <error-page>
    <error-code>403</error-code>
    <location>/error403.xml</location>
</error-page>
<error-page>
    <error-code>404</error-code>
    <location>/error404.xml</location>
</error-page>
<error-page>
    <error-code>405</error-code>
    <location>/error405.xml</location>
</error-page>

当 Tomcat 回答由于 http 错误 404 和 405 导致的错误请求时,正确映射并获取我的文件而不是典型的 html 错误文件。所以 error404.xml 和 error405.xml 显示正确,但我的问题是当客户端尝试连接并导致 403 错误时,Tomcat 没有将答案映射到我的 error403.xml 所以Tomcat 发送空白(空答案)。

有人知道吗?任何答案将不胜感激

我已经解决了我的问题,包括在我的 web.xml 中:

<error-page>
    <error-code>403</error-code>
    <location>/AppExceptionHandler</location>
</error-page>  

<error-page>
    <error-code>404</error-code>
    <location>/AppExceptionHandler</location>
</error-page>

<error-page>
    <error-code>405</error-code>
    <location>/AppExceptionHandler</location>
</error-page>

A​​ppExceptionHandler 是一个 class,当出现 http 错误(403、404、405)时调用。我想要的是当任何评论的 http 错误发生时 Tomcat 回复 xml 而不是 class 的典型 html.The 定义是:

package exception;

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;
import connectionDB.Constants;

@WebServlet("/AppExceptionHandler")
public class AppExceptionHandler extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        processError(request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        processError(request, response);
    }

    protected void doPut(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        processError(request, response);
    }

    protected void doDelete(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        processError(request, response);
    }

    protected void doHead(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        processError(request, response);
    }

    protected void doOptions(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        processError(request, response);
    }

    private void processError(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // Analyze the servlet exception
        //Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        String servletName = (String) request.getAttribute("javax.servlet.error.servlet_name");
        if (servletName == null) {
            servletName = "Unknown";
        }
        String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri");
        if (requestUri == null) {
            requestUri = "Unknown";
        }

        // Set response content type
        response.setContentType("application/xml");

        //Set response content type
        response.setContentType("application/xml");

        PrintWriter out = response.getWriter();
        //<?xml version="1.0" encoding="UTF-8" standalone="yes"?><MW_ERROR_RESPONSE><MW_VERSION>1.0</MW_VERSION><MW_ERRORDESC>Wrong user</MW_ERRORDESC></MW_ERROR_RESPONSE>
        out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><MW_ERROR_RESPONSE><MW_VERSION>" + Constants.MW_VERSION +"</MW_VERSION><MW_ERRORDESC>");
        if(statusCode == 405){
            response.setStatus(405);
            out.write("Client Error Request - HTTP method not allowed. Please change your HTTP method in your request</MW_ERRORDESC></MW_ERROR_RESPONSE>");
        }else if(statusCode == 404){
            response.setStatus(404);
            out.write("Client Error Request - Request not found or invalid URI: " + requestUri  + ". Please change your URI request</MW_ERRORDESC></MW_ERROR_RESPONSE>");
        }else{
            response.setStatus(403);
            out.write("Client Error Request - Unknown error</MW_ERRORDESC></MW_ERROR_RESPONSE>");
        }
    }
}

所以当客户端发送错误请求时,答案是:

<MW_ERROR_RESPONSE>
<MW_VERSION>1.0</MW_VERSION>
   <MW_ERRORDESC>Client Error Request - Request not found or invalid URI: /***/***. Please change your URI request</MW_ERRORDESC>
</MW_ERROR_RESPONSE>