简体   繁体   English

Servlet中的软件包

[英]Packages in Servlets

In Apache tomcat, there was a project created that included Servlets to service the incoming requests. 在Apache tomcat中,创建了一个包含Servlet的项目,以服务传入的请求。 Java class files were kept under Java类文件保存在

WEBINF\classes\com\example\web

Why do the Java files need to include "package com.example.web" statement so that their compiled classes may be kept in com\\example\\web folder? 为什么Java文件需要包含“ package com.example.web”语句,以便它们的编译类可以保存在com \\ example \\ web文件夹中? Tomcat container can easily locate the class files because of the Servlet mappings. 由于Servlet映射,Tomcat容器可以轻松地找到类文件。 Why is this package statement necessary? 为什么需要这个包装声明?

Because that's how the language works. 因为这就是语言的工作方式。 You specify exactly which classes you import in order to avoid ambiguities. 您可以准确指定要导入的类,以避免产生歧义。

Imagine the common scenario when you have two classes with the same name, but different package. 想象一下当您有两个名称相同但包不同的类时的常见情况。 Which one should be picked? 应该选哪一个?

For a namespace . 对于命名空间 Apart from allowing you to have two different classes by the same names (yet making them resolvable), namespaces also help in having a logical group of classes together. 除了允许您使用相同的名称拥有两个不同的类(但仍使它们可解析)之外,名称空间还有助于将一组逻辑上的类组合在一起。 I know I'd look at a util package for utility classes, a domain package for entities, a dao package for data-access classes. 我知道我会查看实用程序类的util包,实体的域包,数据访问类的dao包。 Imagine having them all under one hood without multiple packages. 想象一下,将它们全部放在一个引擎盖下而没有多个包装的情况。 Wouldn't that be a mess ? 那不是一团糟吗?

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

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