Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ServletModule.filter与@WebFilter_Java_Guice_Servlet Filters - Fatal编程技术网

Java ServletModule.filter与@WebFilter

Java ServletModule.filter与@WebFilter,java,guice,servlet-filters,Java,Guice,Servlet Filters,我正在尝试遵循一个关于在web服务器上使用Guice的简单教程,而不需要web.xml: 与本教程的创建者一样,我无法使ServletModule filter命令按预期工作,但所有代码都是相同的,而是在filter类上使用@WebFilter属性会导致web服务器工作 如何使ServletModule筛选器工作?ServletModule的filter方法和@WebFilter属性之间的区别是什么导致了期望值的差异 除了教程中介绍的内容之外,我还尝试在“filter”命令之前绑定过滤器 使用@

我正在尝试遵循一个关于在web服务器上使用Guice的简单教程,而不需要web.xml:

与本教程的创建者一样,我无法使ServletModule filter命令按预期工作,但所有代码都是相同的,而是在filter类上使用@WebFilter属性会导致web服务器工作

如何使ServletModule筛选器工作?ServletModule的filter方法和@WebFilter属性之间的区别是什么导致了期望值的差异

除了教程中介绍的内容之外,我还尝试在“filter”命令之前绑定过滤器

使用@WebFilter(“/*”),我得到了一个简单的响应“helloworld!”


使用过滤器(“/*”),我在同一个请求中得到了404。

据我所知,我所寻找的是不可能的。为了在不使用@WebFilter属性的情况下声明筛选器,您必须具有如下所示的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 id="WebApp_ID" version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Application</display-name>

    <filter>
        <filter-name>guiceFilter</filter-name>
        <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>guiceFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

应用
guiceFilter
com.google.inject.servlet.GuiceFilter
guiceFilter
/*

总之,您可以使用@WebFilter属性,也可以使用web.xml,为了更易于阅读,无法避免这两者。

据我所知,我所寻找的是不可能的。为了在不使用@WebFilter属性的情况下声明筛选器,您必须具有如下所示的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 id="WebApp_ID" version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Application</display-name>

    <filter>
        <filter-name>guiceFilter</filter-name>
        <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>guiceFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

应用
guiceFilter
com.google.inject.servlet.GuiceFilter
guiceFilter
/*
总而言之,您可以使用@WebFilter属性,或者必须使用web.xml,为了更易于阅读配置,无法避免两者