Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.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 关于Servlet绑定模式和默认Servlet_Java_Tomcat_Servlets_Undertow - Fatal编程技术网

Java 关于Servlet绑定模式和默认Servlet

Java 关于Servlet绑定模式和默认Servlet,java,tomcat,servlets,undertow,Java,Tomcat,Servlets,Undertow,我有一个Servlet要绑定到“/”模式。之后,所有URL都可以正常工作,包括应用程序根目录,但我还需要defaultservlet来支持静态文件 在以这种方式将其添加到web.xml中之后,我在应用程序根URL处出现了404错误,但所有其他URL仍然由分配给“/”的Servlet成功处理 违约 *.html *.css *.js 在我将Servlet也绑定到“模式之后,它就可以正常工作了。我对该模式的理解与应用程序的根URL有关 你能帮我找出这种行为的原因吗 另外,我用TomCat和Und

我有一个Servlet要绑定到
“/”
模式。之后,所有URL都可以正常工作,包括应用程序根目录,但我还需要
default
servlet来支持静态文件

在以这种方式将其添加到web.xml中之后,我在应用程序根URL处出现了
404
错误,但所有其他URL仍然由分配给
“/”的Servlet成功处理


违约
*.html
*.css
*.js
在我将Servlet也绑定到
模式之后,它就可以正常工作了。我对该模式的理解与应用程序的根URL有关

你能帮我找出这种行为的原因吗


另外,我用TomCat和Undertow检查行为。

我使用Apache TomCat 9.0.21和以下servlet复制了您的问题:

package my.sample;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Servlet1 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("servlet1: " + req.getServletPath());
    }
}
和web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    id="myApp" version="4.0">
    <display-name>myWebApp</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>servlet1</servlet-name>
        <servlet-class>my.sample.Servlet1</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>servlet1</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.html</url-pattern>
        <url-pattern>*.css</url-pattern>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>
</web-app>

myWebApp
index.html
servlet1
my.sample.Servlet1
servlet1
/
违约
*.html
*.css
*.js
打开URL
http://localhost:8080/myWebApp/
导致您描述的错误404。其他不匹配任何
默认值的url
servlet
url模式
由my
servlet1
处理,并生成类似
servlet1:/hello
的输出,例如在打开
http://localhost:8080/myWebApp/hello

当从
default
servlet映射中删除
*.html
时,您的问题消失了,我在打开
servlet时得到
servlet1://
作为响应http://localhost:8080/myWebApp/

*.html
添加到
servlet1
servlet映射时,打开
servlet1:/index.html
作为响应http://localhost:8080/myWebApp/


这表明您出现问题的原因是路径
/
在内部转发到欢迎文件index.html,该文件与您案例中为
默认
servlet配置的*.html url模式完全匹配,因此选择
默认
servlet来处理
/
请求路径。如果index.html不存在,
default
servlet将发送您观察到的404错误。

不要认为这是一个副本。下面是一个servlet的
*.html
绑定模式影响另一个servlet的
/
绑定模式处理的原因。我如何知道类似于
*.html
的模式是servlet容器模式清单中的最后一个。“正因为如此,我才觉得更奇怪。”塞拉隆,我的问题不是关于任何绑定模式的意义。我知道
”、
“/”
“*
”是什么意思。我想问的是
的行为为什么会这样/“
在我将默认Servlet的绑定添加到
*之后有点不同。如果我只有
/
绑定,那么应用程序根URL也可以处理。但在我将默认Servlet的绑定添加到
*之后行为发生了变化,我需要额外的技巧来绑定根Servlet。我收回了重复的接近投票,并假设这些评论可以删除,因为我最初把你的问题搞错了。非常感谢!与我的期望相比,一切都简单得多。)