How import a .java file into a JSP page?
Solution 1
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.
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 API indicates that you should be importing "twitter4j" namespaces, not "twitter" namespaces.
Solution 2
Question 1, in Eclipse, when I run these files, they are automatically compiled. Where does these class files go?
The class files go inside WEB-INF/classes
directory.
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?
Perhaps you have forgotten to put twitterXXX.jar
(or whatever it is called) into WEB-INF/lib
directory.
He Hui
I am a student that just got into programming very recently. Hi. :)
Updated on June 28, 2022Comments
-
He Hui almost 2 years
this is my first time trying to use java for backend web systems.
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.
However, I am confused with a few things.
I now have a Dynamic Web Project, developing in Eclipse IDE and using TomCat7.0 apache.
I have a directory Java_Resources/src/somepackage/
and some .java files in the above directory.
Question 1, in Eclipse, when I run these files, they are automatically compiled. Where does these class files go?
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?
<%@ 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.
How do you import classes in 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.
All that was required to do, is to move the relevant .jar files into this directory.
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.
Thank you to all that helped answer my question.
This is my final directory image
-
He Hui almost 12 yearstwitter4j is a class i wrote myself actually. sorry for the name confusion.
-
He Hui almost 12 yearsdo u mean that, something.java (a file i wrote myself), I need to create a jar file of that, and put it in WEB-INF/lib directory?
-
adarshr almost 12 yearsNo, the twitter4j API that you're using, that comes in a JAR file, doesn't it? That goes inside the
lib
directory. -
He Hui almost 12 yearsagain, sorry for the confusion. twitter4j is a class i wrote myself. I'll edit the question to avoid further confusion.
-
adarshr almost 12 yearsJust saw your other comment. Can you show what package declaration you have for the Twitter4J class that you've written?
-
Edwin Buck almost 12 yearsNot a problem, but this is exactly why Sun recommends you add organizational information to your namespace. For my hobby stuff, my namespaces look like
edwinbuck.twitter
, and I recommend you do something similar (hehui.twitter
). It is a real pain to do this later because you want to import something that conflicts in namespace. -
adarshr almost 12 yearsOr
org.hehui.twitter
if you end up owningwww.hehui.org
domain :) But then you don't need to own a domain to name your package like that anyway. -
He Hui almost 12 yearsI've changed the question already. Same problem still exists. Do I need to create a WEB-INF/classes, and move my class files there?
-
Edwin Buck almost 12 yearsYes, you do need a WEB-INF/classes, and any imports do need to be there, in subdirectories that match the namespaces. Or if you like JAR files, a WEB-INF/lib needs to contain the JAR file that contains the imported classes.
-
He Hui almost 12 yearsThanks, I got my answer, edited and placed it in my question. Thank you :)