为什么 Java 网络应用程序使用 .do 扩展名?它从哪里来的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3597582/
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
Why do Java webapps use .do extension? Where did it come from?
提问by Adam Gent
I have always wondered why so many Java developers use ".do" as the extension for their web controller (MVC) resources. Example: http://example.com/register.do
我一直想知道为什么这么多 Java 开发人员使用“.do”作为其 Web 控制器 (MVC) 资源的扩展名。示例:http: //example.com/register.do
It doesn't even seem to be framework specific as I have seen it in Spring MVC and Struts projects. Where did this ".do" extension practice come from. Why was this done instead of no extension? I feel like I missed the Java world memo on this.
它甚至不像我在 Spring MVC 和 Struts 项目中看到的那样是特定于框架的。这种“.do”扩展实践从何而来。为什么这样做而不是没有扩展?我觉得我错过了关于这个的 Java 世界备忘录。
Personally I prefer no extension.
我个人更喜欢没有扩展名。
采纳答案by Pascal Thivent
To my knowledge, this convention has been spread by Struts1. The user guide puts it like this:
据我所知,这个约定是由 Struts1 传播的。用户指南是这样写的:
5.4.2 Configure the ActionServlet Mapping
Note:The material in this section is not specific to Struts. The configuration of servlet mappings is defined in the Java Servlet Specification. This section describes the most common means of configuring a application.
There are two common approaches to defining the URLs that will be processed by the controller servlet -- prefix matching and extension matching. An appropriate mapping entry for each approach will be described below.
Prefix matching means that you want all URLs that start (after the context path part) with a particular value to be passed to this servlet. Such an entry might look like this:
<servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>/do/*</url-pattern> </servlet-mapping>
which means that a request URI to match the
/logon
path described earlier might look like this:http://www.mycompany.com/myapplication/do/logon
where
/myapplication
is the context path under which your application is deployed.Extension mapping, on the other hand, matches request URIs to the action servlet based on the fact that the URI ends with a period followed by a defined set of characters. For example, the JSP processing servlet is mapped to the
*.jsp
pattern so that it is called to process every JSP page that is requested. To use the*.do
extension (which implies "do something"), the mapping entry would look like this:<servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
and a request URI to match the
/logon
path described earlier might look like this:http://www.mycompany.com/myapplication/logon.do
WARNING- The framework will not operate correctly if you define more than one
<servlet-mapping>
element for the controller servlet.WARNING- If you are using the new module support since version 1.1, you should be aware that only extension mapping is supported.
5.4.2 配置ActionServlet映射
注意:本节中的材料并非特定于 Struts。Java Servlet 规范中定义了 servlet 映射的配置。本节介绍配置应用程序的最常用方法。
有两种常见的方法来定义将由控制器 servlet 处理的 URL——前缀匹配和扩展匹配。下面将描述每种方法的适当映射条目。
前缀匹配意味着您希望将所有以特定值开头(在上下文路径部分之后)的 URL 传递给此 servlet。这样的条目可能如下所示:
<servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>/do/*</url-pattern> </servlet-mapping>
这意味着匹配
/logon
前面描述的路径的请求 URI可能如下所示:http://www.mycompany.com/myapplication/do/logon
/myapplication
部署应用程序的上下文路径在哪里。另一方面,扩展映射基于 URI 以句点结束,后跟一组定义的字符这一事实将请求 URI 与操作 servlet 匹配。例如,将 JSP 处理 servlet 映射到
*.jsp
模式,以便调用它来处理所请求的每个 JSP 页面。要使用*.do
扩展(这意味着“做某事”),映射条目将如下所示:<servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
与
/logon
前面描述的路径匹配的请求 URI 可能如下所示:http://www.mycompany.com/myapplication/logon.do
警告- 如果您
<servlet-mapping>
为控制器 servlet定义了多个元素,则框架将无法正确运行。警告- 如果您使用自 1.1 版以来的新模块支持,您应该知道仅支持扩展映射。
And I think this convention has been kept (sometimes to not change URLseven after replacing Struts1, sometimes just because people were happy with it).
而且我认为这个约定一直被保留(有时即使在替换 Struts1 之后也不更改 URL,有时只是因为人们对它感到满意)。
回答by leonm
It was common practice to map your struts servlet to *.do in web.xml to pass URLs to the struts servlet. For example:
通常的做法是将您的 struts servlet 映射到 web.xml 中的 *.do 以将 URL 传递给 struts servlet。例如:
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
There is really no reason except convention for this. If you use no extension you need to do some magic to handle images and other static content in a way that doesn't send them to your sevlet. Often this gets done at a load balancer of a fronting web server.
除了惯例之外,真的没有任何理由。如果你不使用扩展,你需要做一些魔法来处理图像和其他静态内容,而不是将它们发送到你的 sevlet。这通常是在前端 Web 服务器的负载均衡器上完成的。
回答by Alireza Fattahi
Just a security tip!
只是一个安全提示!
It is good practice to use some unusual extension for your controller, in this way the intruders will need to spend more time to find some info about the site.
为您的控制器使用一些不寻常的扩展是一种很好的做法,这样入侵者将需要花更多的时间来查找有关该站点的一些信息。
So if you change the default extension, plus some few statics in your framework which may reveal your hand, your MVC framework can be completely unknown.
因此,如果您更改默认扩展名,再加上您框架中的一些可能暴露您的手的静态信息,您的 MVC 框架可能完全未知。
Even change extension to php
or aspx
could be good idea.
甚至将扩展名更改为php
或aspx
可能是个好主意。
Well indeed this is security by obfuscation, but this isn't the opposite of good security. Layering security by obscurity on top of an already secure system might help. There are interesting pros and cons of security by obfuscation and when they can be both used in the internet.
的确,这是通过混淆实现的安全性,但这与良好的安全性并不相反。在已经安全的系统之上通过隐匿性分层安全性可能会有所帮助。通过混淆以及它们何时可以在互联网中使用,安全性有一些有趣的优点和缺点。