How import a .java file into a JSP page?

15,331

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.

Share:
15,331
He Hui
Author by

He Hui

I am a student that just got into programming very recently. Hi. :)

Updated on June 28, 2022

Comments

  • He Hui
    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:

    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

  • He Hui
    He Hui almost 12 years
    twitter4j is a class i wrote myself actually. sorry for the name confusion.
  • He Hui
    He Hui almost 12 years
    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?
  • adarshr
    adarshr almost 12 years
    No, the twitter4j API that you're using, that comes in a JAR file, doesn't it? That goes inside the lib directory.
  • He Hui
    He Hui almost 12 years
    again, sorry for the confusion. twitter4j is a class i wrote myself. I'll edit the question to avoid further confusion.
  • adarshr
    adarshr almost 12 years
    Just saw your other comment. Can you show what package declaration you have for the Twitter4J class that you've written?
  • Edwin Buck
    Edwin Buck almost 12 years
    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.
  • adarshr
    adarshr almost 12 years
    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.
  • He Hui
    He Hui almost 12 years
    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?
  • Edwin Buck
    Edwin Buck almost 12 years
    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.
  • He Hui
    He Hui almost 12 years
    Thanks, I got my answer, edited and placed it in my question. Thank you :)