• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Applet fetching problem in servlet

 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 StringApplet.java:

Then i wrote the servlet names AppletGenerator2.java 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 java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class AppletGenerator2 extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
// Set its title
String title = "Servlet Throwing an Applet to the Client";
out.println("<HTML>");
out.println("<HEAD>");
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>");
out.println("</APPLET>");
out.println("</BODY></HTML>");
}
}

Waiting for ur help, ranchers....
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic