[英]Tomcat can't find class that is placed directly under classes folder
I have the following JSP: 我有以下JSP:
<%@ page import="foo.*" %>
<html>
<body>
The page count is:
<%=Counter.getCount()%>
</body>
</html>
I have a Counter
class in package foo
which is stored in: 我在包
foo
有一个Counter
类,它存储在:
C:\\apache-tomcat-6.0.32\\webapps\\God\\WEB-INF\\classes
C:\\ Apache的Tomcat的6.0.32 \\的webapps \\神\\ WEB-INF \\班
And the container could find the class from its package foo
. 容器可以从它的包
foo
找到类。
But when I try to add some other class file directly under \\WEB-INF\\classes
and not in any specific package such as foo
, then container can't find that class. 但是当我尝试直接在
\\WEB-INF\\classes
下添加一些其他类文件而不是在foo
等任何特定包中添加时,容器找不到该类。
How is this caused and how can I solve it? 这是怎么造成的,我该如何解决?
Classes in the default package are not visible to classes which are by itself inside a package. 默认包中的类对于包本身内的类是不可见的。 You must put the class in a package whenever you want to import it in another class which is by itself inside a package.
每当要将类导入到另一个类中时, 必须将该类放在一个包中,该类本身就在一个包中。 Technically, when JSP files are compiled, the container will autogenerate a
.class
file which is by itself inside a package. 从技术上讲,当编译JSP文件时,容器将自动生成一个
.class
文件,该文件本身就在一个包中。 So you cannot import classes from the default package in the JSP. 因此,您无法从JSP中的默认包中导入类。
So, whenever you want to be able to reuse a class anywhere, it has to be placed in a concrete package, not in the default package. 因此,只要您希望能够在任何地方重用类,就必须将其放在具体的包中,而不是放在默认包中。 As an exercise, create two classes yourself, one which is inside a package and other which is not inside a package.
作为练习,自己创建两个类,一个在包内,另一个不在包内。 Now, inside the one with package, try to
import
and use the one without package. 现在,在带有包的那个内部,尝试
import
并使用没有包的那个。 You'll see that it's not possible and the code won't compile. 你会发现它是不可能的,代码也不会编译。 The servletcontainer encounters exactly the same problem "under the hoods".
servletcontainer在“引擎盖下”遇到完全相同的问题。
Unrelated to the concrete problem: writing raw Java code in JSP files is a poor practice . 与具体问题无关 :在JSP文件中编写原始Java代码是一种不好的做法 。 Consider learning and using servlets .
考虑学习和使用servlet 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.