Java 如何在 web.xml 中将 url 重定向到 jsp

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

how to redirect url to jsp in web.xml

javajspweb-applications

提问by Spiderman

(Should be an easy one-)

(应该是一个简单的-)

How can I redirect all urls of the pattern yada*.js into a my specific jsp.
will this addition to my web.xml work:

如何将模式 yada*.js 的所有 url 重定向到我的特定 jsp。
将这个添加到我的 web.xml 工作:

<servlet-mapping>
        <servlet-name>MySpecific.jsp</servlet-name>
        <url-pattern>yada*.js</url-pattern>
</servlet-mapping>

or perhaps I must use javax.servlet.filter for that purpose?

或者也许我必须为此目的使用 javax.servlet.filter ?

采纳答案by BalusC

I'd drop those files in a folder called /yadaand then use an url-patternof /yada/*.

我就把你的那些文件名为文件夹中/yada,然后使用url-pattern/yada/*

If you don't want to do that, then hand-determining it in a filter is indeed the only resort.

如果您不想这样做,那么在过滤器中手动确定它确实是唯一的手段。



Update: as per your update, you actually have a second question which wasn't directly obvious from your initial question: "How to declare a JSP file as a servlet?". The answer is: use <jsp-file>instead of <servlet-class>.

更新:根据您的更新,您实际上还有第二个问题,这与您最初的问题并不直接:“如何将 JSP 文件声明为 servlet?” . 答案是:用<jsp-file>代替<servlet-class>

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

Nevertheless, as stated in the comments, this is not the best practice. This smells to raw Java code in a JSP file which you should avoidto all extent.

然而,正如评论中所述,这不是最佳实践。这对 JSP 文件中的原始 Java 代码有异味,您应该尽量避免这种情况。

回答by helios

Depending on the server the things that you can put in url-pattern are very limited.

根据服务器的不同,您可以放入 url-pattern 的内容非常有限。

Two are valid:

两个是有效的:

  • an absolute path (no wildcards)
  • *.ext
  • 绝对路径(无通配符)
  • *.ext

So that specification is not matching. I'd use a filter indeed.

所以那个规格不匹配。我确实会使用过滤器。

PS: don't forget to specify <%page sourceEncoding=... contentType=... %> in the generating JSP :). And content-type should include charset=xxxxx

PS:不要忘记在生成的 JSP 中指定 <%page sourceEncoding=... contentType=... %> :)。并且内容类型应该包括 charset=xxxxx