@WebServlet 注释无法识别; init 不运行

发布于 2024-11-28 19:36:37 字数 3973 浏览 1 评论 0 原文

我正在尝试学习注释。我目前有一个 Web 应用程序,当该应用程序在 Tomcat 中启动时,该应用程序会运行 init() 。

以下代码有效...

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <servlet>
        <servlet-name>MainServlet</servlet-name>
        <servlet-class>com.company.Main</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
</web-app>

Main.java:

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;

//@WebServlet(name="MainServlet", value="/main.jsp", loadOnStartup=1)
public class Main extends GenericServlet {

    public Main() { }

    @Override
    public void init() {
        System.out.println("Hello!");
    }

    @Override
    public void destroy() {
        System.out.println("Bye!");
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { }
}

但是,当我取消注释 @WebServlet 注释并注释掉 web.xml 中的 servlet 条目时,init 方法不会运行。

我错过了一些明显的东西吗?

如果这有帮助,这是我的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Name</name>
    <url>http://maven.apache.org</url>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.0.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>3.0.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.6.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>

        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

I'm trying to learn annotations. I currently have a webapp that runs an init() when the app is started in Tomcat.

The following code works...

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <servlet>
        <servlet-name>MainServlet</servlet-name>
        <servlet-class>com.company.Main</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
</web-app>

Main.java:

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;

//@WebServlet(name="MainServlet", value="/main.jsp", loadOnStartup=1)
public class Main extends GenericServlet {

    public Main() { }

    @Override
    public void init() {
        System.out.println("Hello!");
    }

    @Override
    public void destroy() {
        System.out.println("Bye!");
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { }
}

However, when I uncomment the @WebServlet annotation and comment out the servlet entry in web.xml, the init method doesn't run.

Am I missing something obvious?

In case this will help, this is my pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Name</name>
    <url>http://maven.apache.org</url>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.0.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>3.0.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.6.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>

        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

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

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

发布评论

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

评论(3

多彩岁月 2024-12-05 19:36:38

确保您正在 servlet 3.0 容器中运行 - 例如 tomcat 7(tomcat 6 不支持 servlet 3.0),

然后尝试指定 - 默认情况下应该是 false,但请尝试一下。

Make sure you are running in a servlet 3.0 container - tomcat 7, for example (tomcat 6 does not support servlet 3.0)

Then try specifying <web-app metadata-complete="false" /> - it should be false by default, but try it.

赠我空喜 2024-12-05 19:36:38

您的应用程序可能有一个旧格式的 web.xml,使 JBoss 忽略任何带注释的类。因此,如果您的 web.xml 如下所示:

<!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>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

则将其更改为:

<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">
    <display-name>Archetype Created Web Application</display-name>
</web-app>

这将确保 JBoss 再次以 servlet 3.0 的形式处理战争。

Your application might have a web.xml in an old format making JBoss ignore any annotated classes. So if your web.xml looks like:

<!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>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

then change it into:

<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">
    <display-name>Archetype Created Web Application</display-name>
</web-app>

This will make sure JBoss handles the war as servlet 3.0 again.

友谊不毕业 2024-12-05 19:36:38

根据 Servlet 3.0 规范,该类使用 @ WebServlet 注释必须扩展 HttpServlet。这可能意味着您的 GenericServlet 无法实现。

According to the Servlet 3.0 spec, the class using the @WebServlet annotation has to extend HttpServlet. That could mean that your GenericServlet just wont do.

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