Reputation: 1178
I understand handling ServletException with the WEB-INF/web.xml
file using the <error-page>
tag:
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
I've read a several times, that I should avoid using the WEB-INF/web.xml
file in combination with Java Servlet API 3.+.
How do I handle ServletExceptions without the WEB-INF/web.xml
?
Upvotes: 0
Views: 82
Reputation: 5274
You read/understood wrong. Not only are you free to do what you want and it will work, but you can't entirely avoid using the web.xml anyway - like in this case.
The heart of the matter is that modern servlet specifications have useful annotations like @WebServlet
, @WebFilter
and @WebListener
which make it unnecessary to declare such things as servlets, filters and listeners inside the web.xml saving a bit of redundant XML configuration annoyance. They are particularly useful for web frameworks and libraries which can now make things deploy automatically without you having to define anything in the web.xml first.
If you want to use it for your own servlets is still fully up to you, you might argue that having everything configured in the web.xml gives you one place where you can lookup all configuration rather than having to scout for it in several java source files.
There is even a benefit to still declaring things like filters in the web.xml: you can force a specific order of execution since they will be executed in the order that they appear in the web.xml.
So no, do not avoid the web.xml as much as possible. Avoid it when it makes sense.
Upvotes: 3