0

我知道这已经在这里得到了回答,而且都在谷歌上,但我似乎无法让它发挥作用。我想做的是捕获 400,403,404,500 等...

我有一个名为 index 的控制器...

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

    String page = request.getParameter("p");

    if(page == null || page.equalsIgnoreCase("home"))
    {
        RequestDispatcher rd = request.getRequestDispatcher("pages/index.jsp");
        rd.forward(request, response);
    }
    else
    {
        RequestDispatcher rd = request.getRequestDispatcher("pages/error.jsp");
        rd.forward(request, response);          
    }}

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

当有人输入像 localhost:8080/app/index?p=NOT_A_PAGE 这样的 url 时,这工作正常,但如果有人试图用 localhost:8080/app/notMYController?p= 之类的东西破坏 url,我不知道如何捕获错误家或本地主机:8080/app/。

我在这里见过人们,并且在谷歌上说“将以下行添加到位于您的 WEB-INF 文件夹中的 web.xml 文件中:”

<error-page>
    <error-code>500</error-code>
    <location>/pages/error.jsp</location>
</error-page>
<error-page>
    <error-code>404</error-code>
    <location>/pages/error.jsp</location>
</error-page>

现在我尝试了这个,如果我转到“localhost:8080/app/indexs”之类的东西,我仍然会得到正常的 Apache/Tomcat/7.0.12 404 页面。

有人可以告诉我我做错了什么,或者我需要做什么来解决这个问题。谢谢 :)

4

0 回答 0