Reputation: 13083
I have a dynamic web project in eclipse, which uses annotations to map servlets like this:
@WebServlet("/LoginController")
public class LoginController extends HttpServlet {
....
}
I also have jsp pages which uses these servlets, like this:
<form method="post" action="LoginController">
It all work like a char, until I decided to add error pages for errors 404
and 500
. A little search on Google take me to java2s and I find out that I have to modify web.xml
file to specify error pages. But my project doesn't have a web.xml
file under WEB-INF
directory(By the way, I am using Eclipse Indigo as the IDE and Tomcat 7.0.14). So I added one as follows:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<error-page>
<error-code>
404
</error-code>
<location>
/404.html
</location>
</error-page>
<error-page>
<error-code>
500
</error-code>
<location>
/500.html
</location>
</error-page>
</web-app>
This, introduces a new problem. My servlets are not working anymore! All of my JSPs are working fine. But all requests to my servlets take me to the 404 error page. For example, I can get a login form as JSP's are working fine. But, when I submit my login form, ie., to LoginController
it brings up 404 error page.
I think it may be solved by using web.xml
file for servlet mapping instead of WebServlet
annotation. But is there a way to use WebServlet
annotation and also provide error pages? I will be happy if web.xml
can be avoided to show error pages. Also, I don't like to edit any configuration files of tomcat. So, how can I solve the problem?
Upvotes: 0
Views: 1062
Reputation: 691785
Remove the doc type, and use the following web-app element declaration:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
The doc type you use declares your web-app as a 2.3 webapp, which doesn't have support for annotations.
Upvotes: 3