4

I am working with tomcat 7 and I've built and deployed a Spring MVC webapp in tomcat 7 and it's working perfectly fine. What I want is that, whenever a 404 error occurs on my server, it should be redirected to a custom page which I have built in my webapp. I have configured my webapp as a default webapp in tomcat.

I have tried doing this:

<error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/templates/error/error.html</location>
</error-page>

But all in vain.

Glad if someone can help me out in this.

alexbt
  • 16,415
  • 6
  • 78
  • 87
Hitesh
  • 277
  • 2
  • 4
  • 16

3 Answers3

8

You can do something like this:

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

And then:

@Controller
public class ErrorController {

    @RequestMapping("/error404")
    protected String error404() {
        return "/templates/error/error.html";
    }
}
andvicoso
  • 119
  • 1
  • 9
  • @Dejel The first one tells the server to redirect the 404 HTTP error code to the `error404` URL. The second manages the new request for the URL `error404`. – andvicoso Aug 17 '15 at 18:09
4

Since you are already working in WEB-INF(web.xml) folder,You need not to mention WEB-INF.

<error-page> <error-code>404</error-code> 
        <location>/templates/error/error.html</location>
                                                 </error-page>

Check this tutorial also.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Thanks for reply. But I am still facing the same problem. Neither I am getting the default page nor I am redirected to my Custom page. I did this change in web.xml of the tomcat in $CATALINA_HOME/conf . – Hitesh Sep 13 '13 at 08:01
  • 1
    @Hitesh Why you changed in tomcats web.xml??I'm talking about your project's web.xml.Your project don't have any `web.xml` file ??You should do in your projects web.xml not tomcat's one. – Suresh Atta Sep 13 '13 at 08:04
  • Yes I do have and I tried in that also by embedding the above written code just before the tag.But that also doesn't seems to be working. – Hitesh Sep 13 '13 at 09:08
1

Go to your WEB-INF folder > views.

Create a page for example : error_404.jsp.

Now you have to create a controller.

@RequestMapping(value="/error", method = RequestMethod.GET)
public String error_404(){
    return "error_404";
}

Now edit your web.xml file and edit it like this :

<error-page> 
  <error-code>404</error-code> 
  <location>/error</location>
</error-page>
Yassine.b
  • 617
  • 5
  • 11