tomcat - How to send servlet response to web.xml configured error page -


i using tomcat. defined <error-page> in web.xml , mapped 404 error page /error/error.jsp. need detect if resource exist , set response status 404 if resource not available.

response.setstatus(404); 

but tomcat not redirect 404 page defined, question is, there api page location defined in web.xml? don't want parse web.xml myself.

just use httpservletresponse#senderror() status code. e.g.

file resource = new file(path, name); if (!resource.exists()) {     response.senderror(httpservletresponse.sc_not_found); // sends 404.     return; } 

the servletcontainer display suitable errorpage.

note: return statement isn't there decoration. avoid remant of code in same method block continue run , might produce illegalstateexceptions in appserver logs! starters namely think methods sendredirect(), forward(), senderror(), etc somehow automagically exits method block when invoked. not true ;)


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -