如何在jsp页面中导入java类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20904010/
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
how to import java class in jsp page
提问by Solution
Hello Every one i have one jsp page which is located at webapps/ROOT and one java file which is in WEB-INF/classes. i have compiled my java file and get .class file also. now i want to import my class file into jsp page. for this purpose i have create one folder "mypackage" and put my both (class and java) files in it.after this i have try to import into jsp file. for that i have write following code. and mypackage is in WEB-INF/classes folder.
你好,我有一个位于 webapps/ROOT 的 jsp 页面和一个位于 WEB-INF/classes 的 java 文件。我已经编译了我的 java 文件并获得了 .class 文件。现在我想将我的类文件导入jsp页面。为此,我创建了一个文件夹“mypackage”并将我的(类和 java)文件都放在其中。在此之后,我尝试导入到 jsp 文件中。为此,我编写了以下代码。并且 mypackage 在 WEB-INF/classes 文件夹中。
<%@ page import="mypackage.Ps123" %>
here ps123 is my class name. and when i following code
这里 ps123 是我的班级名称。当我遵循代码时
Ps123 p = new Ps123();
i got error
我有错误
Only a type can be imported.mypackage.ps123 resolves to a package
ps123 cannot be resolved to a type
Tell me whats wrong with this code. if i dont import java class in jsp file . it works fine.
告诉我这段代码有什么问题。如果我不在 jsp 文件中导入 java 类。它工作正常。
回答by Stephen C
Lets start with the error message.
让我们从错误消息开始。
Only a type can be imported.mypackage.ps123 resolves to a package
ps123 cannot be resolved to a type
First point to make is that it is not the precise error message. Rather, you appear to have typed it in by hand ... and gotten it a bit wrong. (I find it hard to believe that an error message would be that badly punctuated!)
首先要指出的是,它不是准确的错误消息。相反,您似乎是手动输入的……并且有点错误。(我发现很难相信错误消息的标点符号会如此严重!)
But assuming that the message is essentially correct, then we can infer something significant. The JSP compiler has actually found somethingcalled "mypackage.ps123", but that something is not a .class file that represents a Java class. Furthermore, since the compiler thinksthat "mypackage.ps123" is a package we can infer that:
但是假设消息本质上是正确的,那么我们就可以推断出一些重要的东西。JSP 编译器实际上已经找到了一个叫做“mypackage.ps123”的东西,但那个东西并不是代表 Java 类的 .class 文件。此外,由于编译器认为“mypackage.ps123”是一个包,我们可以推断:
- "WEB-INF/classes/mypackage/ps123" in your deployed webapp is a directory, OR
- the "mypackage.ps123" package has been defined earlier in the classpath; e.g. by something in a Tomcat shared library directory.
- 部署的 webapp 中的“WEB-INF/classes/mypackage/ps123”是一个目录,或者
- “mypackage.ps123”包已在类路径中较早地定义;例如,通过 Tomcat 共享库目录中的某些内容。
The only other explanation I can think of is that your bad choice of classname is confusingthe JSP compiler. If this is happening, then it is a compiler bug. But the fix would be simple, change your class name to conform to the Java style rules for identifiers: a class name shouldalways start with an upper-case letter.
我能想到的唯一其他解释是您对类名的错误选择会混淆JSP 编译器。如果发生这种情况,那么这是一个编译器错误。但是修复很简单,更改类名以符合标识符的 Java 样式规则:类名应始终以大写字母开头。