简体   繁体   English

为何使用 <servlet-mapping> 在jsp / servlet的web.xml(部署描述符)中?

[英]Why use <servlet-mapping> in web.xml(Deployment Descriptor) in jsp/servlet?

<servlet>
    <servlet-name>Hello</servlet-name>
    <servlet-class>HelloWorld</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/HelloWorld.do</url-pattern>
</servlet-mapping>

Why do we use url-pattern inside servlet-mapping tag. 为什么我们在servlet-mapping标签内部使用url-pattern Why not in servlet tag itself. 为什么不在servlet标签本身。

It's seems just an extra tag to write. 这似乎只是一个额外的标签。

Is it just because servlet/jsp spec writers decided to do so or has it some logical reason behind its existance ? 是不是因为servlet / jsp规范编写者决定这样做或者它存在背后的逻辑原因?

This is more likely due to the fact that servlets were intended to support multiple protocols, and not just HTTP. 这很可能是因为servlet旨在支持多种协议,而不仅仅是HTTP。 URL patterns are specific to HTTP alone, and therefore the mapping of the servlet to HTTP URL patterns is done in a servlet-mapping tag, instead of the servlet tag which is used for declaring the more generic properties of the servlet. URL模式仅针对HTTP特定,因此servlet到HTTP URL模式servlet-mapping是在servlet-mapping标记中完成的,而不是用于声明servlet的更通用属性的servlet标记。

u may try to write another that also linked to the same servlet,and then u will know that the servlet can have more than one servlet-mapping. 你可能会尝试编写另一个也链接到同一个servlet的东西,然后你会知道servlet可以有多个servlet映射。

<servlet>
   <servlet-name>Hello</servlet-name>    
   <servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>    
   <servlet-name>Hello</servlet-name>    
   <url-pattern>/HelloWorld.do</url-pattern>
</servlet-mapping>
<servlet-mapping>    
   <servlet-name>Hello</servlet-name>    
   <url-pattern>/HelloWorld2.do</url-pattern>
</servlet-mapping>

I agree with Vineet Reynold that servlet-mapping is used to provide support for different protocols available for communication in network. 我同意Vineet Reynold的说法,servlet-mapping用于为可用于网络通信的不同协议提供支持。 therfore, url-pattern tag let know the servlet the type of protocol ie. 因此,url-pattern标签让servlet了解协议的类型即。 HTTP requesting the service. HTTP请求服务。

The limitations and weaknesses of the standard servlet url pattern was a driving factor behind many of the early MVC frameworks (Struts etc). 标准servlet url模式的局限性和弱点是许多早期MVC框架(Struts等)背后的驱动因素。 No support for regular expression matching, or even ant-style patterns. 不支持正则表达式匹配,甚至不支持ant样式模式。 No support for url exclusion patterns etc etc. 不支持网址排除模式等。

As to why the specification is the way it is (and still continue to be), I have no idea. 至于为什么规范是它的方式(现在仍然如此),我不知道。 The only people who know for sure are the ones who wrote the specs. 唯一知道的人就是编写规范的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM