Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet-Applet communication problem

 
Ravikumar Jambunathan
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
this is my coding,
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class AppletViaServlet extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException {
res.setContentType("text/html");
ServletOutputStream out = res.getOutputStream();
out.println("<html><head><title> Marketers Servlet Application </title></head>");
out.println("<body>");
out.println("<applet code=\"AppletEx.class\" width=500 height=200>");
out.println("</applet>");
out.println("</body></html>");
out.close();
}
}
The applet class "AppletEx.class" is kept inside app. directory 'MyServlets'(outside WEB-INF).
The AppletViaServlet.class in kept inside MyServlets/WEB-INF/classes as well.
My applet is running fine using appletviewer, but when i want to access thru servlet it is not getting initiated.
Need your suggestions pl. thanks.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the browser sees this

It tries to download the class from the perceived location of the page.
In this case, that is based on the URL your servlet is addressed with.
The best thing to do is to not use the relative addressing but completely specify the location of the applet class. You might look into using the BASE tag in your HTML - that helps for locating images, CSS and other resources if you don't want your servlet to serve them directly.
Bill
 
Ravikumar Jambunathan
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Mr.William,
could you give an example to make me clear?, thankyou.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The base tag is used like this:

Now if the browser sees code="some.class" it will request /resources/some.class from the web server.
Same for images src="mypic.gif" will be requested as /resources/mypic.gif
Bill
 
Ravikumar Jambunathan
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thankyou very much Mr.William.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic