wood burning stoves 2.0*
The moose likes Applets and the fly likes Tomcat, JSP and Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Applets
Bookmark "Tomcat, JSP and Applet" Watch "Tomcat, JSP and Applet" New topic
Author

Tomcat, JSP and Applet

marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
Hi,

I am trying to make an applet accessible through a JSP, but it won't load, I have the following error in my browser's Java console:

java.lang.ClassFormatError: Incompatible magic value 1013461310 in class file MyApplet

...

The applet is in the same directory of the JSP that calls it wich is the root of the project. Here's the code in the JSP:
Code:

<jsp: plugin type="applet" codebase="." code="MyApplet.class" width="408" height="410" >
<jsp:fallback>
Plugin tag OBJECT or EMBED not supported by browser.
</jsp:fallback>
</jsp: plugin>


There are no preblems when I call the applet from a usual html file with the applet tag, but I am running Tomcat on port 8080.

What am I doing wrong? Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
Try removing the codebase attribute. Also, does it work if you put the applet tag into the JSP? I recommend doing that anyway, it's more reliable in my experience than whatever the plugin attribute generates.


Ping & DNS - my free Android networking tools app
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
Hello, I think it's not possible to remove the codebase attribute(or at leat, that's what eclipse says).I have also tried with the applet tag but that didn't work. I am in a dispair.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
I have also tried with the applet tag but that didn't work.

So the applet tag works OK if the page name ends in ".html", but not if it ends in ".jsp"? That sounds odd.

It could also indicate that the class file is damaged; this is the exact same file (byte by byte) that works OK within the HTML page? Can you download the class file through the browser manually, and that file then works OK?

Also, please check your private messages.
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
Ulf Dittmer wrote:
So the applet tag works OK if the page name ends in ".html", but not if it ends in ".jsp"?

That's right. But as far as I know, you can't do this

request.getRequestDispatcher("page.html").forward(request, response);

with a html file like you would with a jsp file. So I just double click it and it works. For the jsp however I launch Tomcat and I type in the URL mapped with the servlet that invokes it.

Ulf Dittmer wrote:
It could also indicate that the class file is damaged; this is the exact same file (byte by byte) that works OK within the HTML page?


By "byte by byte" you mean it's the same physical file? If so then yes.



Ulf Dittmer wrote:
Can you download the class file through the browser manually, and that file then works OK?


Sorry I don't understand what you mean. Please tell me how to do this and I will try it.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
marwen Bakkar wrote:
Ulf Dittmer wrote:Can you download the class file through the browser manually, and that file then works OK?

Sorry I don't understand what you mean. Please tell me how to do this and I will try it.

The class file is accessible through HTTP via a URL like http://localhost:8080/myWebApp/myDir/MyApplet.class, assuming that the HTML page is at http://localhost:8080/myWebApp/myDir/MyApplet.html. If you type that into the browser, then the class file should get downloaded, and you can ascertain whether it is, in fact, a byte-by-byte copy of the file that's on the server.
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84

Ulf Dittmer wrote:The class file is accessible through HTTP via a URL like http://localhost:8080/myWebApp/myDir/MyApplet.class, assuming that the HTML page is at http://localhost:8080/myWebApp/myDir/MyApplet.html. If you type that into the browser, then the class file should get downloaded, and you can ascertain whether it is, in fact, a byte-by-byte copy of the file that's on the server.


Well it's not accessible at all

"Project" is the name of my project and for now I have no subfolders and everything is at the root (the MyApplet.class , the test.html file and the jsp)

I also have a single servlet that is mapped like this :

<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


Now when I type http://localhost:8080/Project/test.html in my browser I still have the content of the JSP displayed. I relaunched both the browser and tomcat and I got the same result.

Also, I found an applet in tomcat examples that worked. I copied the jsp and applet in my project and tried accessing and I had the same error message. What could I be doing wrong??
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
Hey mate, I just discovered a very strange thing. I removed the servlet that was calling the jsp and I tried accessing the jsp directly http://localhost:8080/Project/test.jsp and it worked?! what does that mean? And does that entail I should not use a servlet everytime I embed an applet in my jsp?


By the way this the code from the servlet:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Servlet extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{

request.getRequestDispatcher("test.jsp").forward(request, response);
}
}


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
I removed the servlet that was calling the jsp and I tried accessing the jsp directly http://localhost:8080/Project/test.jsp and it worked?! what does that mean? And does that entail I should not use a servlet everytime I embed an applet in my jsp?

No, that's not what it means. The problem is that the servlet is mapped to "/" - that means it handles *all* resources in that web app - images, CSS, JavaScript, JSPs, class files, everything. The servlet should be mapped to exactly those resource URLs that it's supposed to handle, but not to file-based resources that Tomcat itself should serve without going through your web app.
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
Ok so I created a new folder called "folder", put on it the test.jsp and the applet's class file and I changed my servlet mapping like this:

<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/folder</url-pattern>
</servlet-mapping>

But I have Exception : java.lang.ClassNotFoundException: MyApplet.class when I try to access http://localhost:8080/Project/folder

Again I tried http://localhost:8080/Project/folder/test.jsp and it worked
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64

That's the path mapped to the servlet, so, again, you're running into the same problem. Map the servlet to some other path (maybe "/folder2"), and then you can keep the applet stuff in "folder".
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
Well I see the logic now. When you forward from a servlet to a jsp, that is a completely server side forward and the requested URL does not change. Thus any page relative links from the jsp do not necessarily work. From now on I wil specify the path to the applet codebase as an absolute url from the webroot.

Thank you for your answers
 
GeeCON Prague 2014
 
subject: Tomcat, JSP and Applet