为什么servlet映射中的url-pattern以正斜杠(/)开头 [英] Why should url-pattern in servlet mapping start with forward slash(/)

查看:1701
本文介绍了为什么servlet映射中的url-pattern以正斜杠(/)开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Head First JSP和Servlets一书。我正在浏览servlet的映射。我怀疑的是

I was reading Head First JSP and Servlets book. I was going through the mapping of servlet. And my doubt here is

<servlet>
    <servlet-name>test</servlet-name>
    <servlet-class>com.avinash.TestServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>/ServletBeer.do</url-pattern>
</servlet-mapping>




  1. 为什么 url-pattern 以正斜杠开头( / )?

  2. 正斜杠是什么( / )代表什么?它代表我们的webapp名称吗?

  3. 如果 url-pattern 不以正斜杠开头会发生什么( / )?

  4. 这是一个以正斜杠( / )开头的规范吗?

  1. Why should the url-pattern start with forward slash(/)?
  2. What does the forward slash(/) represent? Does it represent our webapp name?
  3. What happens if the url-pattern do not start with forward slash(/)?
  4. Is it a specification to start with forward slash(/)?

在书中明确提到:


不要忘记url-pattern中的正斜杠(/)。

Don't forget the forward slash(/) in the url-pattern.

你能解释一下吗?

推荐答案

url-pattern中的 / 表示使用此功能的Web应用程序的根目录web.xml中。所以当你给 /ServletBeer.do 时。这意味着 http://< yourhost>:< port> /<您的上下文Root> /ServletBeer.do 。当你点击这个url时,将调用你用这个 com.avinash.TestServlet 映射的servlet。

The / in the url-pattern means the root of the web application that uses this web.xml. So when you give /ServletBeer.do. That means http://<yourhost>:<port>/<your context Root>/ServletBeer.do. When ever you hit this url, the servlet you which have mapped with this i.e com.avinash.TestServlet will be invoked.

您必须在所有网址映射中使用 / 并非强制性 - 这完全取决于您希望如何配置它。 / 只是一个正则表达式。您还可以在url-pattern中使用 * .jsp - 每次在Web应用程序中请求任何jsp时,它都会实际调用此servlet。
考虑** JSR-000315 Java Servlet 3.0最终版本 12.2映射规范**

Its not mandatory that you have to use / in all your url mapping - that purely depends on how you want to configure it. / is just a regular expression. You can also use *.jsp in your url-pattern - which will actually invoke this servlet everytime you request any jsp in your web- application. Consider the section 12.2 Specification of Mappings ** from **JSR-000315 Java Servlet 3.0 Final Release


12.2映射规范在Web应用程序部署描述符中,以下语法用于定义映射:

12.2 Specification of Mappings In the Web application deployment descriptor, the following syntax is used to define mappings:

■以/字符开头并以字符结尾的字符串带有'/ *'
后缀用于路径映射。

■ A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping.

■以'*。'前缀开头的字符串用作扩展名
mapping。

■ A string beginning with a ‘*.’ prefix is used as an extension mapping.

■空字符串()是一个特殊的URL模式,它完全映射到应用程序的上下文根
,即请求表格
http:// host:port /< contextroot> / 。在这种情况下,路径信息是'/'
,servlet路径和上下文路径是空字符串()。

■ The empty string ("") is a special URL pattern that exactly maps to the application's context root, i.e., requests of the form http://host:port/<contextroot>/. In this case the path info is ’/’ and the servlet path and context path is empty string ("").

■仅包含'/'字符表示应用程序的默认
servlet。在这种情况下,servlet路径是
请求URI减去上下文路径,路径信息为null。

■ A string containing only the ’/’ character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.

■所有其他字符串仅用于完全匹配

■ All other strings are used for exact matches only

这篇关于为什么servlet映射中的url-pattern以正斜杠(/)开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆