简体   繁体   English

如何将.java文件导入JSP页面?

[英]How import a .java file into a JSP page?

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: 我一直在阅读一些关于如何做到这一点的指南,到目前为止我理解以下几点:

  1. I am not allowed to import .java files, but rather, import the class files. 我不允许导入.java文件,而是导入类文件。
  2. <%@ page language="java" import="somefile.*"%> this is how i import a package. <%@ page language =“java”import =“somefile。*”%>这是我导入包的方式。
  3. After importing a package, I am required to create an instance of the class. 导入包后,我需要创建该类的实例。

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. 问题1,在Eclipse中,当我运行这些文件时,它们会自动编译。 Where does these class files go? 这些类文件在哪里?

Question 2, when I run the following code, I recieved some errors. 问题2,当我运行以下代码时,我收到了一些错误。 What am I doing wrong? 我究竟做错了什么? Is it because I do not have my class files in the right directory? 是因为我在正确的目录中没有我的类文件吗?

<%@ 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文件的解决方案。

http://www.jsptut.com/ http://www.jsptut.com/

How do you import classes in JSP? 如何在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 这是我的最终目录图片 在此输入图像描述

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; 从快速浏览API,您的导入似乎正确格式化; but, you are importing the wrong namespace. 但是,您导入的名称空间错误。 The javadoc for the twitter4j API indicates that you should be importing "twitter4j" namespaces, not "twitter" namespaces. twitter4j APIjavadoc表示您应该导入“twitter4j”命名空间,而不是“twitter”命名空间。

Question 1, in Eclipse, when I run these files, they are automatically compiled. 问题1,在Eclipse中,当我运行这些文件时,它们会自动编译。 Where does these class files go? 这些类文件在哪里?

The class files go inside WEB-INF/classes directory. 类文件位于WEB-INF/classes目录中。

Question 2, when I run the following code, I recieved some errors. 问题2,当我运行以下代码时,我收到了一些错误。 What am I doing wrong? 我究竟做错了什么? Is it because I do not have my class files in the right directory? 是因为我在正确的目录中没有我的类文件吗?

Perhaps you have forgotten to put twitterXXX.jar (or whatever it is called) into WEB-INF/lib directory. 也许您忘记将twitterXXX.jar (或其他任何名称)放入WEB-INF/lib目录中。

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

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