eclipse 如何将 .java 文件导入 JSP 页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10739400/
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 import a .java file into a JSP page?
提问by He Hui
this is my first time trying to use java for backend web systems.
这是我第一次尝试将 java 用于后端 Web 系统。
I have been reading up some guides on how to do this, and thus far i understand the following points:
我一直在阅读有关如何执行此操作的一些指南,到目前为止,我了解以下几点:
- I am not allowed to import .java files, but rather, import the class files.
- <%@ page language="java" import="somefile.*"%> this is how i import a package.
- After importing a package, I am required to create an instance of the class.
- 我不允许导入 .java 文件,而是导入类文件。
- <%@ page language="java" import="somefile.*"%> 这就是我导入包的方式。
- 导入包后,我需要创建类的实例。
However, I am confused with a few things.
但是,我对一些事情感到困惑。
I now have a Dynamic Web Project, developing in Eclipse IDE and using TomCat7.0 apache.
我现在有一个动态 Web 项目,在 Eclipse IDE 中开发并使用 TomCat7.0 apache。
I have a directory Java_Resources/src/somepackage/
我有一个目录 Java_Resources/src/somepackage/
and some .java files in the above directory.
以及上述目录中的一些 .java 文件。
Question 1, in Eclipse, when I run these files, they are automatically compiled. Where does these class files go?
问题1,在Eclipse中,当我运行这些文件时,它们会被自动编译。这些类文件去哪儿了?
Question 2, when I run the following code, I recieved some errors. What am I doing wrong? Is it because I do not have my class files in the right directory?
问题 2,当我运行以下代码时,收到一些错误。我究竟做错了什么?是不是因为我的类文件没有放在正确的目录中?
<%@ page language="java" import="twitter.*"%>
<%
JavaFile jf = new Javafile();
String result = jf.searchTweets("someString");
System.out.println(result);
%>
Error report:
错误报告:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /hello.jsp at line 10
7: <%@ page language="java" import="twitter.*"%>
8:
9: <%
10: Javafile jf = new JavaFile();
11:
12: String result = jf.searchTweets("someString");
13: System.out.println(result);
Thank you for your time and effort.
感谢您的时间和努力。
note: I have read the following links, but they do not seem to provide a solution on how to write your own .java files and import these classes into your jsp files.
注意:我已阅读以下链接,但它们似乎没有提供有关如何编写您自己的 .java 文件并将这些类导入您的 jsp 文件的解决方案。
How do you import classes in JSP?
http://www.coderanch.com/t/288534/JSP/java/importing-class-file-jsp
http://www.coderanch.com/t/288534/JSP/java/importing-class-file-jsp
EDIT: I found my own answer.
编辑:我找到了我自己的答案。
This is my original directory
这是我的原始目录
As you can see, my WEB-INF/lib is empty.
如您所见,我的 WEB-INF/lib 是空的。
All that was required to do, is to move the relevant .jar files into this directory.
所需要做的就是将相关的 .jar 文件移动到此目录中。
Note: I have already imported the relevant .jar files to the Eclipse project, but it seems that I need to move them into WEB-INF/lib on top of importing them into the project.
注意:我已经将相关的 .jar 文件导入到了 Eclipse 项目中,但是在将它们导入到项目中的基础上,我似乎还需要将它们移动到 WEB-INF/lib 中。
Thank you to all that helped answer my question.
感谢所有帮助回答我的问题的人。
This is my final directory image
这是我的最终目录图像
采纳答案by Edwin Buck
Only classes may be imported into a .java
file, and a JSP "compiles" to a .java
file, so this requirement holds for JSP files too.
只能将类导入到.java
文件中,而 JSP 会“编译”到.java
文件中,因此该要求也适用于 JSP 文件。
From a quick glance at the API, it seems that your import is formatted correctly; but, you are importing the wrong namespace. The javadoc for the twitter4j APIindicates that you should be importing "twitter4j" namespaces, not "twitter" namespaces.
快速浏览一下 API,似乎您的导入格式正确;但是,您导入了错误的命名空间。twitter4j API的javadoc表明您应该导入“twitter4j”命名空间,而不是“twitter”命名空间。
回答by adarshr
Question 1, in Eclipse, when I run these files, they are automatically compiled. Where does these class files go?
问题1,在Eclipse中,当我运行这些文件时,它们会被自动编译。这些类文件去哪儿了?
The class files go inside WEB-INF/classes
directory.
类文件进入WEB-INF/classes
目录。
Question 2, when I run the following code, I recieved some errors. What am I doing wrong? Is it because I do not have my class files in the right directory?
问题 2,当我运行以下代码时,收到一些错误。我究竟做错了什么?是不是因为我的类文件没有放在正确的目录中?
Perhaps you have forgotten to put twitterXXX.jar
(or whatever it is called) into WEB-INF/lib
directory.
也许您忘记将twitterXXX.jar
(或任何名称)放入WEB-INF/lib
目录中。