File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Applet fetching problem in servlet" Watch "Applet fetching problem in servlet" New topic
Author

Applet fetching problem in servlet

Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

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....


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
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: 2370

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Applet fetching problem in servlet
 
Similar Threads
Servlet HelloWorld problem
Applet-servlet communication
package issue
Servlet-Applet communication problem
Passing values through html