Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't lunch applet from a Servlet?

Status
Not open for further replies.

TomBarns

Programmer
Feb 14, 2001
10
US
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=&quot;applet&quot; code=&quot;JspApplet.class&quot; codebase=&quot;&quot; width=&quot;200&quot; height=&quot;200&quot; >
</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(&quot;text/html&quot;);
ServletContext xt =this.getServletContext();
RequestDispatcher dispatcher = xt.getRequestDispatcher(&quot;/jsp/practice/hello.html&quot;);
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(&quot;Click&quot;);
add(b1);
b1.addActionListener(new moh(this));
this.setBackground(Color.cyan);
//this.getParameter(&quot;matt&quot;);


}
public String see()
{
return &quot;mattseif&quot;;
}



}
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(), &quot; }
catch (MalformedURLException ee)
{
}

}
}





//============================
 
When you launch from browser(via directory path) it works because your class file is in the same directory as HTML page. HOWEVER, when using the 'forward' method of RequestDispatcher, the client(browser) does not make a new connection to that URL. The process is carried out on the server, meaning that you would have to make the applet code value relative to the server root:

code=&quot;/jsp/practice/JspApplet.class&quot;

The other option(if it is better, you decide) is to use the response.sendRedirect(&quot;/jsp/practice/hello.html&quot;) method instead of RequestDispatcher forwarding.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top