添加错误页面会阻止 tomcat 7 中的所有 servlet

发布于 2024-12-23 16:07:53 字数 1656 浏览 1 评论 0原文

我在 Eclipse 中有一个动态 Web 项目,它使用注释来映射 servlet,如下所示:

@WebServlet("/LoginController")
public class LoginController extends HttpServlet {
    ....
}

我还有使用这些 servlet 的 jsp 页面,如下所示:

<form method="post" action="LoginController">

一切都像 char 一样工作,直到我决定为错误添加错误页面 404 和 500。在 Google 上进行一点搜索,将我带到 java2s ,我发现我必须修改 web.xml 文件来指定错误页面。但我的项目在 WEB-INF 目录下没有 web.xml 文件(顺便说一下,我使用 Eclipse Indigo 作为 IDE 和 Tomcat 7.0.14) 。所以我添加了一个如下:

<?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>

这引入了一个新问题。我的 servlet 不再工作了!我的所有 JSP 都工作正常。但是对 servlet 的所有请求都会将我带到 404 错误页面。例如,我可以获得一个登录表单,因为 JSP 工作正常。但是,当我向 LoginController 提交登录表单时,它会显示 404 错误页面。

我认为可以通过使用 web.xml 文件进行 servlet 映射而不是 WebServlet 注释来解决。但是有没有一种方法可以使用 WebServlet 注释并提供错误页面呢?如果可以避免 web.xml 显示错误页面,我会很高兴。另外,我不喜欢编辑tomcat的任何配置文件。那么,我该如何解决这个问题呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我乃一代侩神 2024-12-30 16:07:53

删除 doc 类型,并使用以下 Web 应用程序元素声明:

<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">

您使用的 doc 类型将您的 Web 应用程序声明为 2.3 Web 应用程序,该应用程序不支持注释。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文