File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Applet fetching problem in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Applet fetching problem in servlet" Watch "Applet fetching problem in servlet" New topic

Applet fetching problem in servlet

Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2373

Hi Pals,
I was trying to write a servlet that picks an applet from somewhere in the web server and fetches it to the html page it generates dynamically. Fetching an applet in a servlet should only require HTML code AFAIK. So i wrote the following applet code in

Then i wrote the servlet names under "mysite" package which i kept the compiled class file in "<tomcat_instl_dir>/webapps/examples/web-inf/classes" directory creating a folder named mysite.
But when access the servlet thru Tomcat4 it says ClassNotFoundException stating that StringApplet is not found. I went to thru many trial-and-error to copy the class files, packaging them etc...But all in vain... A JSP page finds the applet so easily, but why the servlet is torturing me?
Here is the code for the servlet. please help me finding where am i making the mistake.

package mysite;
import javax.servlet.*;
import javax.servlet.http.*;
public class AppletGenerator2 extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
PrintWriter out = res.getWriter();
// Set its title
String title = "Servlet Throwing an Applet to the Client";
out.println("<TITLE>" + title + "</TITLE>");
out.println("</HEAD>"); // Start on the body
out.println("<BODY BGCOLOR= #6DA987 TEXT=blue>");
out.println("<H2>"+ title+"</H2>");
out.println("<APPLET CODE=\"StringApplet\" WIDTH=300 HEIGHT=300>");

Waiting for ur help, ranchers....

Ashik Uzzaman
Senior Software Engineer, TubeMogul, Emeryville, CA, USA.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
It is really very simple. The browser sees the JSP as originating at a URL where your applet class (and image files, etc.) are also located.
The browser sees a servlet as orginating at some alias URL - which is probably not a real url and certainly not a location that the server can serve class (or image) files from.
The solution is to either:
1. use an absolute URL for all resources
2. put a <BASE tag in the <HEAD area of the generated page to tell the browser how to build a url that can be served by the web server.
As I recall, the syntax is:
<BASE href="/somelegalURL/" />
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2373

Thnx a lot William!
I tried ur first option but somehow managed to miss the absoulute URIs.... ...but as i m confirm now that the problem is not in other places i'll c ut now.
Yah, i remember that i used BASE in Head a few months back when working with HTML but yet managed to forget it again... ...will try the bith approach to test.
I agree. Here's the link:
subject: Applet fetching problem in servlet
It's not a secret anymore!