3

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:

  1. I am not allowed to import .java files, but rather, import the class files.
  2. <%@ page language="java" import="somefile.*"%> this is how i import a package.
  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.

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.

http://www.jsptut.com/

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

enter image description here

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 imageenter image description here

Community
  • 1
  • 1
He Hui
  • 2,196
  • 4
  • 29
  • 46

2 Answers2

3

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.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • twitter4j is a class i wrote myself actually. sorry for the name confusion. – He Hui May 24 '12 at 14:28
  • Not 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. – Edwin Buck May 24 '12 at 14:32
  • Or `org.hehui.twitter` if you end up owning `www.hehui.org` domain :) But then you don't need to own a domain to name your package like that anyway. – adarshr May 24 '12 at 14:34
  • I've changed the question already. Same problem still exists. Do I need to create a WEB-INF/classes, and move my class files there? – He Hui May 24 '12 at 14:35
  • 1
    Yes, 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. – Edwin Buck May 24 '12 at 14:46
  • Thanks, I got my answer, edited and placed it in my question. Thank you :) – He Hui May 24 '12 at 15:05
1

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.

adarshr
  • 61,315
  • 23
  • 138
  • 167
  • do 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? – He Hui May 24 '12 at 14:30
  • No, the twitter4j API that you're using, that comes in a JAR file, doesn't it? That goes inside the `lib` directory. – adarshr May 24 '12 at 14:30
  • again, sorry for the confusion. twitter4j is a class i wrote myself. I'll edit the question to avoid further confusion. – He Hui May 24 '12 at 14:31
  • Just saw your other comment. Can you show what package declaration you have for the Twitter4J class that you've written? – adarshr May 24 '12 at 14:31