Please post the exceptiontext.

I think you get a SecurityException.

Better work with a webserver and try to get the url with getCodebase().

Browsers only allow applets to connect to the server they are loaded from.
So when you want to connect to a different server the applet will not work.


-----Urspr�ngliche Nachricht-----
Von: A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[EMAIL PROTECTED]]Im Auftrag von
Shaila C D
Gesendet: Donnerstag, 25. Mai 2000 11:16
An: [EMAIL PROTECTED]
Betreff: Applet-Servlet communication


Hi,

  I am facing a problem with the communication between an Applet and a
Servlet. This code works fine if the applet is run from an appletviewer,
however, the applet does not fetch the same result when it is run on the
browser. Instead, it throws an exception. The code is attached below. The
whole exercise is to get the time from the server. I am using JSDK2.0,
JDK1.2 and servletrunner to load the servlet.
Let me know if I am missing anything

Thanks.

Applet Code
--------------------

import java.applet.Applet;
import java.net.*;
import java.io.*;
import java.io.Reader.*;
import java.awt.*;

public class client extends Applet {

  URL url = null;
  public void init() {
   try {
    url = new URL("http://localhost:8080/servlet/survey");
   }catch(MalformedURLException e) {
    System.out.println("exception URL");
   }
  }

  public void start() {
    repaint();
  }

  public void paint(Graphics g) {
    g.drawString(getDate(),50,50);
  }

  public String getDate() {
   try {
     URLConnection con = url.openConnection();
     con.setDoInput(true);
     con.setDoOutput(true);
     con.setUseCaches(false);
     InputStream in = con.getInputStream();
     BufferedReader dis = new BufferedReader(new InputStreamReader(in));
     String date = dis.readLine();
     in.close();
     return date;
   }catch(Exception e) {
     System.out.println("exception");
     return null;
   }
  }
}

Servlet code
-------------------
import java.util.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;

public class SurveyServlet extends HttpServlet {
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res) {
    res.setContentType("text/html");
    try {
      PrintWriter toClient = res.getWriter();
      toClient.println(new Date().toString());
    }catch(IOException e) {
       System.out.println("exception");
    }
  }
}

HTML
-----------
<html>
<body>
<applet code=client height=200 width=300> </applet>
</body>
</html>

Servlet.properties
------------------------------
servlet.survey.code=SurveyServlet

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to