Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't lunch applet from within servlet ??  RSS feed

 
Tom Barns
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
When i run the html file(has reference to an applet) from within the browser as (c:\jakarta-tomcat3.2.1\.....\jsp\practice\hello.html)
it runs fine,I have all files within the folder (practice).
But when i try to lunch that file(hello.html) from within the servlet i can't lunch the applet.
i got error that says: " Jspapplet class not found ".
Here is all my files:
hello.html
//======================
<html>
<applet type="applet" code="JspApplet.class" codebase="" width="200" height="200" >
</applet>

</html>
//=====================
testServlet.java
//=====================
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class testServlet extends HttpServlet
{

public void service (HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException
{
//PrintWriter out=response.getWriter();
//response.setContentType("text/html");
ServletContext xt =this.getServletContext();
RequestDispatcher dispatcher = xt.getRequestDispatcher("/jsp/practice/hello.html");
dispatcher.forward( request,response);

}
}
//=============================
JspApplet.java
//============================
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.net.*;

public class JspApplet extends Applet
{

String s;
public void setString(String s)
{
this.s=s;
}


public void init()
{
Button b1 = new Button("Click");
add(b1);
b1.addActionListener(new moh(this));
this.setBackground(Color.cyan);
//this.getParameter("matt");


}
public String see()
{
return "mattseif";
}



}
class moh implements ActionListener
{
JspApplet j;
public moh(JspApplet j)
{
this.j=j;
}
public void actionPerformed(ActionEvent e)
{

try
{

j.getAppletContext().showDocument(new URL(j.getDocumentBase(), "http://localhost:8080/examples/jsp/practice/hello.jsp"));
}
catch (MalformedURLException ee)
{
}

}
}




//============================
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is simple, your applet tag
<applet type="applet" code="JspApplet.class" codebase="" width="200" height="200" >
</applet>
gives the codebase="" - since your browser is getting the page from the servlet, it thinks it can get the class file from the servlet too. The fix is either:
create a <base> tag for the page giving the location of the class files (presumably the URL you used when testing with plain html)
or set codebase to the correct URL.
I prefer the base method because that will also fix all references to image or other resource files.
Bill

------------------
author of:
 
Tom Barns
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your time,it works with codebase attribute,but how do we use <base> tag with our html file?
thanks again.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a good HTML reference - I recommend the "Hip Pocket Guide to HTML 4" - the <base> tag goes in the <head>..</head> area of an HTML page - example:
<base href="http://www.javaranch.com/somedirectory/" >
This defines where the browser will start looking for resources such as image files, sounds, etc etc. that are defined with a relative address in a page.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!