Java 如何使用 web.xml 映射 Html 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31358286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:57:52  来源:igfitidea点击:

How to map Html files with web.xml?

javahtmljspservlets

提问by chanchal karn

Ok i tried to find this answer everywhere. What im trying to do here is mapping an html file with a url..? Yes i can access the html file by using app/page.html. But i want to map it to a different url.

好的,我试图到处找到这个答案。我想在这里做的是用一个 url 映射一个 html 文件..?是的,我可以使用 app/page.html 访问 html 文件。但我想将它映射到不同的网址。

I know how to map servlet classes and jsp files , but i dont know how to map html files.

我知道如何映射 servlet 类和 jsp 文件,但我不知道如何映射 html 文件。

Here's a normal login.htmli want to map:

这是login.html我要映射的正常情况:

<body>
    <form name="loginForm" method="post" action="/Logging..in">
        Username:<input type="text" name="user" />
        <br/>
        PassWord:<input type="password" name="pass">
        <br/>
        <input type="submit" value="Sign Up!">
    </form>
</body>

回答by hagrawal

Here you go. URL as /app/pagewill show your page.html

干得好。URL as/app/page将显示您的page.html

<servlet>
    <servlet-name>page</servlet-name>
    <jsp-file>/page.html</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>page</servlet-name>
    <url-pattern>/app/page</url-pattern>
</servlet-mapping>

Please notethat page.htmlshould be directly inside your context root or in other words directly in your *.war.

请注意,它page.html应该直接在您的上下文根中,或者直接在您的 *.war 中。

Change <url-pattern>/app/page</url-pattern>to any URL of your wish and URL with request will be redirected towards page.html

更改<url-pattern>/app/page</url-pattern>为您希望的任何 URL 和带有请求的 URL 将被重定向到page.html

Key thing to note is that - "In jsp-fileelement, you can specify a JSP or HTML, whose content can be resolved to HTML form."

需要注意的关键是——“jsp-file元素中,你可以指定一个JSP或HTML,其内容可以解析为HTML形式。

And that is the same reason that in <welcome-file>element you can specify either a HTML or JSP file.

这与在<welcome-file>element 中可以指定 HTML 或 JSP 文件的原因相同。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明