开发者

Packages in Servlets

In 开发者_运维百科Apache tomcat, there was a project created that included Servlets to service the incoming requests. Java class files were kept under

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? Tomcat container can easily locate the class files because of the Servlet mappings. 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. Imagine having them all under one hood without multiple packages. Wouldn't that be a mess ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜