You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Haroon Ghaus <ha...@ascotdrummond.co.uk> on 2001/08/10 14:10:47 UTC

Applet Servlet Communication

I am trying to access a servlet through an applet.

In the applet I am using URL Connection object and opening a connection to the servlet.
My URL in the connection object is http://localhost:8080/examples/servlet/DbServlet
and I have kept my DbServlet class in the D:\Tomcat\webapps\examples\WEB-INF\classes\DbServlet.class. I am using tomcat 1.2. Still when I use the Applet in an html page I get the following error 
java.io.FileNotFoundException: http://localhost:8080/examples/servlet/DbServlet

Can anyone help me out.....

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class DbApplet extends Applet implements ActionListener {
 TextField tfQuery;
 TextArea taResults;
 Button btnExecute;

public void actionPerformed(ActionEvent ae) {
 executeQuery();
}

public void executeQuery() {
 String qryString = tfQuery.getText();
 try{
  URL url = new URL(http://localhost:8080/examples/servlet/DbServlet);
  String qry = URLEncoder.encode("qry")+"="+ URLEncoder.encode(qryString);
  URLConnection uc = url.openConnection();
  uc.setDoOutput(true);
  uc.setDoInput(true);
  uc.setUseCaches(false);
  uc.setRequestProperty("Content-type","application/x-www-form-urlencoded");

  DataOutputStream dos = new DataOutputStream(uc.getOutputStream());
  dos.writeBytes(qry);
  dos.flush();
  dos.close();

  InputStreamReader in = new InputStreamReader(uc.getInputStream());
  
  int chr = in.read();
  while (chr!= -1) {
   taResults.append(String.valueOf((char) chr));
   chr = in.read();   
  }

  in.close();   
 }
 catch(MalformedURLException e){
  taResults.setText(e.toString());
 }
 catch(IOException e){
  taResults.setText(e.toString());
 } 
}
public void init() {
 super.init();
 // insert code to initialize the applet here
 Panel p1 =new Panel();
 p1.setLayout(new FlowLayout(FlowLayout.LEFT));
 p1.add (new Label("QueryString:"));

 tfQuery = new TextField("",50);
 p1.add(tfQuery);

 btnExecute = new Button ("Execute Query");
 btnExecute.addActionListener(this);
 p1.add(btnExecute);

 add("North",p1);
 taResults = new TextArea(10,80);
 add("Center",taResults);
}
}


Haroon

Email: Haroon.Ghaus@AscotDrummond.co.uk