java web.xml 中 html 文件的 url 模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29744396/
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-11-02 15:50:27  来源:igfitidea点击:

url pattern for html file in web.xml

javahtmlservletsweb.xmlurl-pattern

提问by Selva

we know how to set url patternfor servletbut I am unable to set url patternfor html in web.xml, can u help me to find solution, I googled but, can't able to get it, please find below for my problem.

我们知道如何为servlet设置url 模式,但我无法在web.xml 中为 html设置url 模式,你能帮我找到解决方案吗,我用谷歌搜索但无法得到它,请在下面找到我的问题.

<servlet>
    <servlet-name>Login</servlet-name>
    <servlet-class>auth.Login</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/login</url-pattern>
</servlet-mapping>

in above code I am setting url patternfor **Login**servlet class in web.xml, like wise can I able to set url patternfor html file in web.xmlpls help to find solution thank you in advance

在上面的代码中,我正在为web.xml 中的servlet 类设置url 模式,同样明智地我可以在web.xml 中为 html 文件设置url 模式, 请帮助找到解决方案,提前谢谢**Login**

回答by Jafar Ali

URL pattern are for servlet and filters. For servlet

URL 模式用于 servlet 和过滤器。对于 servlet

<servlet-mapping>
    <servlet-name>Servlet-name</servlet-name>
    <url-pattern>/< Pattern ></url-pattern>
</servlet-mapping>

For Filter

对于过滤器

<filter-mapping>
    <filter-name>Filter-Name</filter-name>
    <url-pattern>/< Pattern ></url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

Those are not for Html file. Infact there are no pattern configuration for JSPs too.

这些不适用于 Html 文件。事实上,JSP 也没有模式配置。

回答by justAbit

If you want to protect *.html files from direct access (by placing *.html files under WEB-INF) you can use a Servlet which would be only responsible for forwarding all such requests to intended html files.

如果您想保护 *.html 文件免于直接访问(通过将 *.html 文件放在 WEB-INF 下),您可以使用一个 Servlet,它只负责将所有此类请求转发到预期的 html 文件。

<servlet>
    <servlet-name>HTMLServlet</servlet-name>
    <servlet-class>my.package.HTMLServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>HTMLServlet</servlet-name>
    <url-pattern>/somepath/*.html</url-pattern>
</servlet-mapping>

Code in servlet class may look like this

servlet 类中的代码可能如下所示

...
protected void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws ServletException, IOException {
  String requestedPath = //... code for getting requested HTML path
  request.getRequestDispatcher(requestedPath).forward(request, response);
}
...

回答by Alexander Mann

If you don't mind to change your HTML page to JSP you can set url pattern for it like this:

如果您不介意将 HTML 页面更改为 JSP,您可以像这样设置 url 模式:

<servlet>
    <servlet-name>Error</servlet-name>
    <jsp-file>/pages/error.jsp</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>Error</servlet-name>
    <url-pattern>/error</url-pattern>
</servlet-mapping>