使用 web.xml 进行 Servlet 映射
我对 servlet 映射的 web.xml 结构感到困惑,执行它没有任何问题,但我试图弄清楚为什么我们在部署描述符中有这样的模式。
<web-app>
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-path>foo.Servlet</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/enroll</url-pattern>
</servlet-mapping>
</web-app>
现在,据我了解,每当请求 url-pattern“/enroll”时,servlet 容器都会将 servlet-name 与 url-pattern 进行匹配,并尝试找到相应的 servlet-path 并将控制转发到foo.Servlet。所以基本上会有两遍,一个用于查找 servlet-name,另一个用于 servlet-path,我的问题是容器是否设计为按以下方式工作
<web-app>
<servlet>
<servlet-name>foo.Servlet</servlet-path>
<url-pattern>/enroll</url-pattern>
</servlet>
</web-app>
如果我们使用以下方法会有什么缺点?这样岂不是效率更高,响应时间也更快?
I have a confusion regarding the structure of the web.xml for the servlet mapping, I don't have any problem executing it but I am trying to figure out why we have such a pattern in the deployment descriptor.
<web-app>
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-path>foo.Servlet</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/enroll</url-pattern>
</servlet-mapping>
</web-app>
Now as far as my understanding whenever a request comes for url-pattern "/enroll", servlet container is going to match the servlet-name with the url-pattern and will try to find the corresponding servlet-path and will forward the control to foo.Servlet. so basically there would be two passes one for finding servlet-name and another for servlet-path, my question is if container is designed to work in the following way
<web-app>
<servlet>
<servlet-name>foo.Servlet</servlet-path>
<url-pattern>/enroll</url-pattern>
</servlet>
</web-app>
What would be the drawback if we used the following approach? Wouldn't that be more efficient and the response time would be fast?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它允许 servlet 具有多个 servlet 映射:
它允许过滤器映射到特定的 servlet:
您的建议将不支持它们。请注意,
web.xml
仅在应用程序启动期间读取和解析一次,而不是像您想象的那样在每个 HTTP 请求上读取和解析。从 Servlet 3.0 开始,出现了
@WebServlet
注释可最小化此样板文件:
另请参阅:
It allows servlets to have multiple servlet mappings:
It allows filters to be mapped on the particular servlet:
Your proposal would support neither of them. Note that the
web.xml
is read and parsed only once during application's startup, not on every HTTP request as you seem to think.Since Servlet 3.0, there's the
@WebServlet
annotation which minimizes this boilerplate:See also: