任何人都可以解释servlet映射?(Can anyone explain servlet mapping?)

编程入门 行业动态 更新时间:2024-10-24 14:19:55
任何人都可以解释servlet映射?(Can anyone explain servlet mapping?)

我正在使用SpringMVC编写一个Web应用程序。 通常我会将一些补充文件扩展名映射到Spring的前端控制器,并且幸福地生活,但是这次我要使用类似REST的URL,没有文件扩展名。

将我的上下文路径下的所有内容映射到前台控制器(让我们称之为“ 应用程序 ”)意味着我也应该关心静态文件,而我不想做的事情(为什么要重新创建另一个weel?),所以有一些与tomcat默认的组合servlet(让我们称之为“ tomcat ”)似乎是要走的路。

我得到了这样的事情做的事情

<servlet-mapping>
  <servlet-name>app</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

<servlet-mapping>
  <servlet-name>tomcat</servlet-name>
  <url-pattern>*.ext</url-pattern>
</servlet-mapping>
 

并为我的静态内容的每个文件扩展名重复后一个。 我只是想知道为什么以下设置,对我来说相当于上面的设置,不起作用。

<!-- failed attempt #1 -->
<servlet-mapping>
  <servlet-name>app</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
  <servlet-name>tomcat</servlet-name>
  <url-pattern>*.ext</url-pattern>
</servlet-mapping>

<!-- failed attempt #2 -->
<servlet-mapping>
  <servlet-name>app</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

<servlet-mapping>
  <servlet-name>tomcat</servlet-name>
  <url-pattern>/some-static-content-folder/*</url-pattern>
</servlet-mapping>
 

有人可以散发光吗?

I'm trying to write a web application using SpringMVC. Normally I'd just map some made-up file extension to Spring's front controller and live happily, but this time I'm going for REST-like URLs, with no file-name extensions.

Mapping everything under my context path to the front controller (let's call it "app") means I should take care of static files also, something I'd rather not do (why reinvent yet another weel?), so some combination with tomcat's default servlet (let's call it "tomcat") appears to be the way to go.

I got the thing to work doing something like

<servlet-mapping>
  <servlet-name>app</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

<servlet-mapping>
  <servlet-name>tomcat</servlet-name>
  <url-pattern>*.ext</url-pattern>
</servlet-mapping>
 

and repeating the latter for each one of the file extensions of my static content. I'm just wondering why the following setups, which to me are equivalent to the one above, don't work.

<!-- failed attempt #1 -->
<servlet-mapping>
  <servlet-name>app</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
  <servlet-name>tomcat</servlet-name>
  <url-pattern>*.ext</url-pattern>
</servlet-mapping>

<!-- failed attempt #2 -->
<servlet-mapping>
  <servlet-name>app</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

<servlet-mapping>
  <servlet-name>tomcat</servlet-name>
  <url-pattern>/some-static-content-folder/*</url-pattern>
</servlet-mapping>
 

Can anyone shed some light?

最满意答案

我想我可能会知道发生了什么。

在您的工作web.xml中,您将servlet设置为默认servlet(如果没有其他匹配,则本身就是默认的servlet调用),它会回答任何与其他映射不匹配的请求。

在失败1中,您的/ *映射似乎是一个有效的路径映射。 使用web.xml中的/ *映射,它将回复除了其他路径映射之外的所有请求。 根据规范扩展映射是由显式映射覆盖的隐式映射。 这就是扩展映射失败的原因。 一切都明确地映射到应用程序。

在失败2中,应用程序负责所有内容,但与静态内容映射匹配的内容除外。 显示我在快速测试中发生了什么。 这是一个例子。 /some-static-content-folder/ contains test.png

试图访问test.png我试过:

/some-static-content-folder/test.png

并且找不到该文件。 但是尝试

/some-static-content-folder/some-static-content-folder/test.png

它出来了 所以似乎Tomcat的默认servlet(至少6.0.16)会丢弃servlet映射,并尝试使用剩余的路径找到该文件。 根据这篇文章Servlet为静态内容提供 Jetty给出了我和我期待的行为。

有没有一些原因,你不能做一些像地图一个根目录的休息电话。 像app /映射到/ rest_root / *的东西比对rest_root文件夹中的任何内容负责,但是除了你做另外一个显式映射之外,Tomcat还要处理其他任何内容。 我建议将您的休息servlet设置为路径映射,因为它更好地声明了意图。 使用/或/ *似乎不合适,因为你必须绘制出异常。 以SO为例,我的静态映射就是这样

/ users / *

/ posts / *的帖子servlet

映射顺序

显式(路径映射) 隐式(扩展映射) 默认(/)

请更正任何错误的内容。

I think I may know what is going on.

In your working web.xml you have set your servlet to be the default servlet (/ by itself is the default servlet called if there are no other matches), it will answer any request that doesn't match another mapping.

In Failed 1 your /* mapping does appear to be a valid path mapping. With the /* mapping in web.xml it answers all requests except other path mappings. According to the specification extension mappings are implicit mappings that are overwritten by explicit mappings. That's why the extension mapping failed. Everything was explicitly mapped to app.

In Failed 2, App is responsible for everything, except content that matches the static content mapping. To show what is happening in the quick test I set up. Here is an example. /some-static-content-folder/ contains test.png

Trying to access test.png I tried:

/some-static-content-folder/test.png

and the file was not found. However trying

/some-static-content-folder/some-static-content-folder/test.png

it comes up. So it seems that the Tomcat default servlet (6.0.16 at least) drops the servlet mapping and will try to find the file by using the remaining path. According to this post Servlet for serving static content Jetty gives the behavior you and I were expecting.

Is there some reason you can't do something like map a root directory for your rest calls. Something like app mapped to /rest_root/* than you are responsible for anything that goes on in the rest_root folder, but anywhere else should be handled by Tomcat, unless you make another explicit mapping. I suggest setting your rest servlet to a path mapping, because it declares the intent better. Using / or /* don't seem appropriate, since you have to map out the exceptions. Using SO as an example, my rest mappings would be something like

/users/* for the user servlet

/posts/* for the posts servlet

Mapping order

Explicit (Path mappings) Implicit (Extension mappings) Default (/)

Please correct anything that I got wrong.

更多推荐

文件,servlet-mapping>,I'm,扩展名,电脑培训,计算机培训,IT培训"/> <meta name

本文发布于:2023-04-28 03:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329814.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:任何人都可以   servlet   mapping   explain

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!