java 使用 .html 文件作为 JSP

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

Using .html files as JSPs

javajsptomcatjakarta-eeglassfish

提问by jd.

This may be a silly question but I've found no answer when googling this.

这可能是一个愚蠢的问题,但我在谷歌搜索时没有找到答案。

Currently, I map the requests from someFileName.html to a servlet which then forwards to someFileName.jsp using servlet mappings in web.xml. I would like to avoid that and just configure my application server so that html files are parsed and executed as if they were JSPs (so that custom tags and EL could be used from within the HTML). Bonus to answers that allow any extensions to be mapped to the JSP processor.

目前,我将来自 someFileName.html 的请求映射到一个 servlet,然后使用 web.xml 中的 servlet 映射转发到 someFileName.jsp。我想避免这种情况,只配置我的应用程序服务器,以便像 JSP 一样解析和执行 html 文件(以便可以在 HTML 中使用自定义标记和 EL)。对允许将任何扩展映射到 JSP 处理器的答案的奖励。

I use Tomcat but I'd like the solution to be portable to other containers such as Glassfish.

我使用 Tomcat,但我希望该解决方案可移植到其他容器,例如 Glassfish。

采纳答案by fasseg

With 2 simple steps you can achieve this:

通过 2 个简单的步骤,您可以实现这一目标:

  1. Add this servletmapping for the JSP servlet:

    <servlet-mapping>
        <servlet-name>jsp</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    

    This tells the application container to use the the JSP servlet when serving html files.

  2. Comment out the <mime-mapping>for text/htmlmime type (*.html) files so that the container won't handle HTML files as static content.

  1. 为 JSP servlet 添加这个 servletmapping:

    <servlet-mapping>
        <servlet-name>jsp</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    

    这告诉应用程序容器在提供 html 文件时使用 JSP servlet。

  2. 注释掉<mime-mapping>for text/htmlmime 类型 (*.html) 文件,以便容器不会将 HTML 文件作为静态内容处理。

Hope this helps.

希望这可以帮助。