Include java applet class to my web page - java

How can I include my java applet class into my web page using JSP on a netbeans web application project.
I don't want it as a jar. When I use a jar it works correctly.
I have a package named "com.example" which is the applet named Scan.java
Also have a web page named main.jsp which I added the code below.
I am using this code to include the class but
<APPLET CODE="Scan.class" WIDTH=150 HEIGHT=250>
On the browser the applet doens't open and it says "ClassNotFoundException"
How to fix that?

The class "Scan" should be on the same directory than your HTML/JSP page, or you could specify the relative path (from the JSP) to reach your Scan.class with the attribute CODEBASE.
<APPLET CODE="Scan.class" CODEBASE="../classes/com/example" WIDTH=150 HEIGHT=250>

Related

Applet html embedding and I can't seem to locate the .class from my git repo

I am trying embed a java applet into my webpage. Unfortunately, I can't put my .class file straight into the server so, I was going to read the .class file from a git repository and pointing to it with the archive attribute in my html code.
This is the html code:
<html>
<body>
<applet code="DodgemApplet.class" width="640" height="480" archive="https://github.com/smithg017/repo.git">
</applet>
</body>
</html>
I am still pretty new to html so can someone show me the right way I should be going to get my java applet embedded into my website? Thanks!
Well, there was a hidden property I didn't know about and after much digging I found the property codebase.
I put this:
codebase="https://github.com/smithg017/repo/blob/master/"
right after
code = "DodgemApplet.class"
code is simply the name. by default it will look in the same directory as the html file. In my case, in the Desktop folder. If your class file is elsewhere, reference the folder your class file is in under the codebase property.

How to make use of external JavaScript source file in JSP through simple java class

I am creating one project in which I am making call to a JavaScript function which resides in index.jsp from my java class ( .java file). As in this context we shouldnt write <script> tag in our index.jsp, what should I do if I want to make use of some dependent .js file like stomp.js source file in my index.jsp file??

ClassNotFoundException when loading Applet in Java Web Application

I'm trying to deploy Java Web Application (Spring, Hibernate, Maven, Tomcat, WinXp) with a very simple applet, but when I open jsp page with this applet I see ClassNotFountException Error.
The structure of my project (deployed):
myApp
|--META-INF
|--WEB-INF
|--classses
|--ru
|--mydomain
|--applet
|--FileChooserApplet.class
|--views
|--main.jsp
|--index.html
|--resources
FileChooserApplet.class:
package ru.mydomain.applet;
import java.applet.Applet;
public class FileChooserApplet extends Applet {
#Override
public void paint(java.awt.Graphics g) {
g.drawString("Weather is good!", 70, 70);
}
}
main.jsp:
...
<body>
<APPLET code="ru.mydomain.applet.FileChooserApplet.class"
codebase="../classes" width=350 height=200></APPLET>
...
</body>
..
I tried to change codebase attribute to:
"classes"
"/classes"
""
delete this attribute
But,
if i add the same code for applet to index.html and double-click on this file (URL in browser starts with file:///C:/projects/myApp/target/myApp/...) then applet works.
AFAIK the applet will not have access to class files in WEB-INF/classes. These classes can only be accessed by server side resources such as servlets (as opposed to downloadable/static content).
You can jar all the class required for the applet to work and place the JAR file in the views folder. Your applet tag will look like
<APPLET code="ru.mydomain.applet.FileChooserApplet.class"
archive="mynewjar.jar"
width=350 height=200>
</APPLET>
A single JAR file is a cleaner way to do deployments.

How to run applet in web application

I'm having the exact same problem stated in run applet in web application. It throws a ClassNotFoundException for my applet. I tried the solution from there but still no luck.
Here is my code for embedding Applet in html:
<body>
<applet codebase="/DaaS/applet" archive="/DaaS/applet/firstApplet.jar" code="FirstApplet.class" width="300" height ="300"> </applet>
I've a folder DaaS/applet which contains firstApplet.jar and my index.html is in Daas/Webcontent.
The URL in the codebase (and archive) attribute is relative to the current request URL (the one as you see in browser address bar), not to the disk file system in the server side. Imagine that you've the index.html page in some subfolder like so:
http://localhost:8080/somecontext/index.html
The URL as you have in the codebase (and archive) attribute starts with a leading slash / which makes it relative to the domain root instead of the current folder. So the webbrowser will look for the archive and the JAR in the following URL
http://localhost:8080/DaaS/applet/firstApplet.jar
This may not be correct per se. You need to make sure that the codebase (and archive) URL points to the right URL relative to the current request URL. Based on the information given so far, the /DaaS folder is basically in the same parent as index.html, so this should do:
<applet codebase="DaaS/applet" archive="firstApplet.jar" ... />
(note that I simplified the archive attribute, it will be resolved relative to codebase anyway)
This way the browser will load the JAR from:
http://localhost:8080/somecontext/DaaS/applet/firstApplet.jar

How to specify correctly codebase and archive in Java applet?

I use firefox version > 3.5 (3.5.,3.6.,4.*) and I try to specify archive and codebase property correctly but it doesn't work. My main class for applet is situated in the archive and some necessary classes that are loaded during runtime are situated in the codebase. If I specify only the archive then the applet is loaded but the classes from codebase are missing. If I specify the archive and the codebase then the applet can't be loaded. It looks like applet try to load main class from codebase folder and doesn't look into the archive file.
<html>
<body>
<applet width=600 height=300 code="MyClass.class"
type="application/x-java-applet;jpi-version=6"
archive="http://myurl.com/archive/myjar.jar"
codebase="http://myurl.com/classes">
no applet
</applet>
</body>
</html>
Main class is situated in http://myurl.com/archive/myjar.jar and runtime classes are situated in http://myurl.com/classes.
Attribute codebase specifies the base URL of the applet - the directory that contains the applet's code. It is used while searching jar files in archive attribute, in such a way that all jars in archive attribute are searched relative to codebase.
So. When you use archive="http://myurl.com/archive/myjar.jar" and codebase="http://myurl.com/classes" together it means: find "http://myurl.com/archive/myjar.jar" in "http://myurl.com/classes" folder.
I.e. the full search path is "http://myurl.com/classes/http://myurl.com/archive/myjar.jar". And of course it can't be found!
Also, classes, whose jar-files aren't specified in the archive attribute, can't be found without codebase attribute. I.e. if there is no codebase then there is no way to find your classes in "http://myurl.com/classes" folder.
You can find more details in the Deploying With the Applet Tag tutorial.
I suggest the following solution:
Place myjar.jar in the http://myurl.com/classes folder;
Assuming your MyClass.class is in default package, and in the "http://myurl.com/archive/myjar.jar", the following code should work:
<html>
<body>
<applet width=600 height=300 code="MyClass"
type="application/x-java-applet;jpi-version=6"
archive="myjar.jar"
codebase="http://myurl.com/classes">
no applet
</applet>
</body>
</html>

Categories

Resources