Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JSP and the fly likes Applet in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Applet in JSP" Watch "Applet in JSP" New topic
Author

Applet in JSP

Jonathan Blok
Greenhorn

Joined: Oct 16, 2009
Posts: 7
Hello,

I am currently developing a web-application in Eclipse with JBOSS, but I am having trouble displaying an applet in one of my jsp pages. When I open the page I get a ClassNotFoundException. I think this is due to the visibility of the applet as I cannot place images in my pages either. I have read that this is because of the fact that the pages, images and applet are located in my WEB-INF folder, and this folder is protected. Is this correct?

I assumed that placing files outside the WEB-INF folder should resolve this problem but so far it has not.

This is what my structure looks like now:
WebContext
>META-INF
>WEB-INF
>web
-GraphApplet.class
-logo.gif
-result.jsp

When I load result.jsp the following code:

<jsp:plugin type="applet" code="GraphApplet" codebase="web/" jreversion="1.6" width="400" height="300" >
<jsp:fallback>
Plugin tag OBJECT or EMBED not supported by browser.
</jsp:fallback>
</jsp:plugin>

Results in this error:

java.lang.ClassNotFoundException: GraphApplet
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.getBytes(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.access$000(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
... 7 more
Error: java.lang.ClassNotFoundException: GraphApplet
java.lang.ClassFormatError: Incompatible magic value 0 in class file GraphApplet
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Error: java.lang.ClassFormatError: Incompatible magic value 0 in class file GraphApplet

I think the problem is mainly that I don't fully understand where I should put my class files in respect to my jsp pages. I also read that 'Incompatible magic value' often is caused by different java versions, but I do not think this is the case, as I still cannot use images in my jsp page therefore I think it is a visibility problem. Any help on this matter would very much appreciated.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41114
    
  45
Welcome to JavaRanch.
I think the problem is mainly that I don't fully understand where I should put my class files in respect to my jsp pages.

Sort of :-) Best practice is to keep the class files (and images, jar files, and whatever other files the applet uses) in the same directory as the HTML/JSP page that has the applet tag. Which you already have - so that's good. But you need to remove the "codebase" attribute - what that's telling the browser is to look in a subdirectory of the current directory; so effectively the browser is looking for web/web/GraphApplet.class instead of web/GraphApplet.class. You only need a codebase attribute if the class files are not in the same directory as the HTML/JSP page.


Ping & DNS - my free Android networking tools app
Jonathan Blok
Greenhorn

Joined: Oct 16, 2009
Posts: 7
Hello Ulf, thank you for your reply.

I changed the codebase attribute as follows:

<jsp:plugin type="applet" code="GraphApplet" codebase="" jreversion="1.6" width="400" height="300" >

Because Eclipse indicated that codebase was a required attribute, I changed the value to "" instead of removing the attribute. This resulted in a new (slightly confusing) error. There is still a class not found exception but it is accompanied by a number of errors related to applets and graphics and the likes. Should I assume that my applet is causing this and continue my debugging there?

Exception in thread "AWT-EventQueue-4" java.lang.NullPointerException: component argument pData
at sun.java2d.windows.GDIBlitLoops.nativeBlit(Native Method)
at sun.java2d.windows.GDIBlitLoops.Blit(Unknown Source)
at sun.java2d.pipe.DrawImage.blitSurfaceData(Unknown Source)
at sun.java2d.pipe.DrawImage.renderImageCopy(Unknown Source)
at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
at sun.java2d.pipe.ValidatePipe.copyImage(Unknown Source)
at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Laden: Klasse GraphApplet nicht gefunden
java.lang.ClassNotFoundException: GraphApplet
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: open HTTP connection failed:http://localhost:8080/GraphApplet.class
at sun.plugin2.applet.Applet2ClassLoader.getBytes(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.access$000(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
... 7 more
Ausnahme: java.lang.ClassNotFoundException: GraphApplet
Exception in thread "AWT-EventQueue-4" java.lang.NullPointerException: component argument pData
at sun.java2d.windows.GDIBlitLoops.nativeBlit(Native Method)
at sun.java2d.windows.GDIBlitLoops.Blit(Unknown Source)
at sun.java2d.pipe.DrawImage.blitSurfaceData(Unknown Source)
at sun.java2d.pipe.DrawImage.renderImageCopy(Unknown Source)
at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
at sun.java2d.pipe.ValidatePipe.copyImage(Unknown Source)
at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

This line "Caused by: java.io.IOException: open HTTP connection failed:http://localhost:8080/GraphApplet.class" also strikes me as strange, why is the page looking for the class there? The url to the page that the applet is on is "http://localhost:8080/EPSTestWeb/ResultServlet".

Any suggestions?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41114
    
  45
codebase is not a required attribute, no matter what Eclipse thinks. I'm not sure if it's defined what should happen if it is present but empty, but I wouldn't be surprised if browser JVMs were confused about it. So, remove it and see what happens.

Since GraphApplet is the main applet class, and it's not even found, all the other exceptions will likely go away once that issue is resolved.

http://localhost:8080/EPSTestWeb/ResultServlet

That is a problem, because now the applet directory has no relationship with the page URL. So you'll *need* a codebase attribute now - try "/EPSTestWeb".
Jonathan Blok
Greenhorn

Joined: Oct 16, 2009
Posts: 7
The following plugin tag (I switched to a different applet as to be sure the applet wasn't causing any of the problems, I now use a simple example applet)



Still causes the same error, it now searches here (please disregard the difference in capitalization of the project name compared to my earlier post, this one is correct):

Caused by: java.io.IOException: open HTTP connection failed:http://localhost:8080/EPStestWeb/test.class

The servlet that is called by the url 'http://localhost:8080/EPStestWeb/ResultServlet' calls the super.doGet which checks if a user is authenticated (which I am not) and forwards appropriately. This led me to believe the codebase attribute in the 'jsp: plugin' tag should be something like 'codebase="/EPStestWeb/web"' but this did not help either.



Another thing that I find strange is that when I change the jsp tag to
<jsp:plugin type="applet" code="test.class" codebase="/EPStestWeb" jreversion="1.6" width="400" height="300" >

It returns this:
Caused by: java.io.IOException: open HTTP connection failed:http://localhost:8080/EPStestWeb/web/test/class.class
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41114
    
  45
Caused by: java.io.IOException: open HTTP connection failed:http://localhost:8080/EPStestWeb/test.class

OK, so where IS the applet class? Under which URL can you download it manually in the browser?

Another thing that I find strange is that when I change the jsp tag to
<jsp:plugin type="applet" code="test.class" codebase="/EPStestWeb" jreversion="1.6" width="400" height="300" >
It returns this:
Caused by: java.io.IOException: open HTTP connection failed:http://localhost:8080/EPStestWeb/web/test/class.class

That's actually not strange at all. The "code" attribute takes a fully-qualified class name, not a file name. So if the JVM sees a dot, it assumes that the part before it ("test") must a package name, which is therefore translated to a directory when trying to load the class file.
Jonathan Blok
Greenhorn

Joined: Oct 16, 2009
Posts: 7
That's actually not strange at all. The "code" attribute takes a fully-qualified class name, not a file name. So if the JVM sees a dot, it assumes that the part before it ("test") must a package name, which is therefore translated to a directory when trying to load the class file.


Ah, I see.

OK, so where IS the applet class? Under which URL can you download it manually in the browser?


Well that is essentially the problem, I cannot. I thought it was because of it being in the WEB-INF folder at first, but now it's outside the folder and I still have no way of accessing it via the server. I am only able to view pages if they are forwarded to me from a servlet, and I don't know why that is. Do I need to add something to my web.xml? Right now it's only mapping servlets.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41114
    
  45
You don't need to set up anything to serve static files (like applet class files, images, CSS, JavaScript etc.) After pointing the browser at them the server should stream them, and all should be well.

I still have no way of accessing it via the server.

Does that mean the files aren't present where they should be, or that they are present but the server won't serve them, maybe for permissions reasons ? Those would result in different error messages.

To get this to work it would be advisable to completely forget about Eclipse, and just work the the standalone servlet container.
Jonathan Blok
Greenhorn

Joined: Oct 16, 2009
Posts: 7
Thanks for the help so far.

Well I am using the server in Eclipse so I guess to figure this out I'll have to set up a regular application server.

I tried creating a work-around with this servlet:



Which allowed me to access the static files, but when using them to import the applet I was still receiving errors. Using this servlet probably causes some security problems as well.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41114
    
  45
I think you're making this harder than it needs to be. Getting Tomcat up and running to properly serve a web app is quite easy.
Jonathan Blok
Greenhorn

Joined: Oct 16, 2009
Posts: 7
Hi,

After setting up Tomcat I got the applet working and now it also works in my eclipse-integrated JBOSS. I'm not sure what did the trick but correctly setting the codebase attribute definitely helped.

Thanks for the help!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Applet in JSP
 
Similar Threads
Calling an applet in a JSP page
weblogic 10.3 + jdk6.0.14 = java applet problem?
Cannot load applet
Incompatible magic value
a simple examples cannot work.