How import a .java file into a JSP page? - java

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.
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
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

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.

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.

Related

How do I import my own class in another folder into a jsp file?

In the same directory as my index.jsp, I have a folder called 132b.
Inside 132b I have a folder called classes which contains a bunch of Java files.
In my index.jsp, I have tried:
<%# page import="132b/classes/*" %>
<%# page import="132b.classes.*" %>
The first attempt complained about an unexpected end of comment.
The 2nd attempt couldn't find the class I was trying to instantiate so it couldn't be resolved to a type.
How do I properly import/include the classes I wrote?
By default it will search Java files inside src folder till you explicitly set it to be something else..
Try putting 132b folder inside src folder and see if <%# page import="132b.classes.*" %> works.
I always prefer to import only classes which are needed in that particular page instead of importing all the classes of a package.

Embedding multi-class applet with .jar

I have a multi-class applet that has been exported using Eclipse as a .jar file. The jar file is called chess.jar, and the class I compiled and ran from in Eclipse is called Chess.java, and the binaries are Chess.class. The following code is the HTML I am trying to use to embed this applet into my website.
<APPLET CODE=Chess.class
ARCHIVE="chess.jar"
WIDTH=700
HEIGHT=700
CODEBASE="mywebsitewherethefilesarebeinghosted.com/"
ALT="Your browser understands the <APPLET> tag but isn't running the applet, for some reason.">
Your browser is ignoring the <APPLET> tag!
</APPLET>
I get the error ClassNotFoundException Chess.class
I have done applets before that have only one class successfully here , and I am using almost exactly the same HTML except for the CODEBASE and I have added the ARCHIVE tag.
As an applet, it has no main class. I am not very familiar with the MANIFEST.MF file, and I'm not sure if I need to utilize it for this purpose. My Chess.class calls some other classes like Pawn.class and Knight.class and they are all in chess.jar. Any help would be greatly appreciated.
EDIT
I created a local HTML file that runs the .jar locally and it runs perfectly.
<html>
<applet code=Chess.class
archive="chess.jar"
width=700 height=700>
</applet>
</html>
I think it is a simple matter of the contents of your HTML. I see you have a second start tag that I think would be confusing the browser. The browser would think the first one does not have an end tag and that may be the reason it is being ignored. Remove that and try again.
Even if this isn't the problem it's bad practice to leave tags open like that. Your error message should also be a bit more meaningful for poor users who don't know what an applet tag is.
It seems that CODEBASE follows / as directory separator unlike . in case of CODE attribute
so try replacing your CODEBASE value with proper directory structure separated by /
check example here and here also

Applet on Website seems to hate Packages

So I made an Applet (not JApplet) and proceeded to upload it to my website. I put all of my .class files into a package, exported the project from eclipse to a .jar, and uploaded that to the public_html folder of my website.
In my HTML code, I put
<applet ARCHIVE="BallShooter.jar" CODE="BallShooter" width=500 height=500> </applet>
However, it seems that I kept on getting the error "ClassNotFoundException"
If I reupload the .jar WITHOUT the package, it worked fine. Could somebody please explain to me how to fix this?
For those who are wondering, this is the structure of the things in the website
/public_html/myAppletJar/myPackage/a.class
/public_html/myAppletJar/myPackage/b.class
/public_html/myAppletJar/a.png
/public_html/myAppletJar/b.png
Check all these points:
if the applet class is BallShooter, and is in the package kikiotsuka, then its source code must start with the line package kikiotsuka;. The full name of the class is thus kikiotsuka.BallShooter.
in the jar file, you should thus have a directory named kikiotsuka at the root, and this directory must contain a file named BallShooter.class
since the name of the class is kikiotsuka.BallShooter, that's what the code attribute of the applet HTML element must contain: code="kikiotsuka.BallShooter" width=...

Opening input file using java code in JSP

I am using JSP on NetBeans.
In the java code, I am trying to read data from a file. First I open the file by specifying its path.
Because my code runs on a server (GlassFish), I would like to have my file path independent of the machine where it runs. Therefore, I want to start the path with the folder name that contains the file which is saved on the root of the project directory. I tried so hard to achieve that but I couldn't.
I read online and I found this way, but it still doesn't work:
<%
//building the tree here.
GraphBuilder tree = new GraphBuilder("${pageContext.request.contextPath}\\src\\java\\Database\\OptimizedFullTermFile.pad");
%>
Can anyone help? Thank you.
Just read it from the classpath. Given that you're using the typical src folder representing the Java source code (the Java package/class structure), I assume that the file OptimizedFullTermFile.pad is placed in the Java package java.Database (eek, a capital in package name? lowercase it all). In that case, it's already in the classpath and thus you can just get it straight from there as follows:
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("java/Database/OptimizedFullTermFile.pad");
// ...
As to your failed attempt: the EL ${pageContext.request.contextPath} isn't ever going to work in a scriptlet. Even if it did, it's not the right thing, it returns the context path in the webapp URL which is absolutely not part of the local disk file system path, let alone the classpath. Using scriptlets is strongly discouraged since a decade, by the way.
See also:
getResourceAsStream() vs FileInputStream
Where to place and how to read configuration resource files in servlet based application?
You can try the following code to get your directory file in project from the following code.
this.getServletContext().getRealPath("")+[your file path in project web directory]

Not able to import jar files in JSP code

I have jar files which I constructed using command
jar -cf Generic.jar /java/mypackage/*.class
There are three classes in the Generic.jar
mypackage is the name of package I named so as to include my class files and create a jar file.
latter I copied the Generic.jar in the WEB-INF/lib of Glassfish
In JSP I imported these jar files using
<%# page import="java.mypackage.GenericTree;" %>
<%# page import="java.mypackage.GenericTree1111;" %>
<%# page import="java.mypackage.GenericTree2222;" %>
But I am getting the following error. Can anyone suggest whats going wrong?
HTTP Status 500 -
type Exception report
message
descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NoClassDefFoundError: java/mypackage/GenericTree
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.0.1 logs.
GlassFish Server Open Source Edition 3.0.1
Are you sure your command succeeded? It does not work for me when the path starts with slash. So, first remove leading slash, i.e. command should look like the following: jar -cf Generic.jar java/mypackage/*.class
Package name that starts from word java is not for you. It is for JDK classes only. Such classes cannot be loaded by regular class loader, only by bootstrap one. So, rename your package. It should look like com.mycompany.myprogram.foo.bar
Now create the jar file using command I wrote and then test the result: run jar vft and see that output looks like
1612 Thu Feb 03 14:44:34 IST 2011 com/mycompany/myprogram/Hello.class
Pay attention on the path. It must contain your package.
If everything is working copy your jar under WEB-INF/lib and enjoy. Good luck.

Categories