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/13 07:55:52 UTC

Applet to Servlet

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 3.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

RE: Applet to Servlet

Posted by "F.M.Parvez" <fm...@dkf.de>.
Hai,
I have used somthing to same, the code is as following.
	URLConnection servletConnection=null;
		String webServerStr = "http://" + hostName + ":" + port + servletPath;
		String servletGet="";
		try{
			servletGet = webServerStr += "?";
			for(int i=0;i<strargs.length;i++){
				//if(i==strargs.length-1){
				//   webServerStr+=strargs[i][0]+"="+ URLEncoder.encode(strargs[i][1]);
				//}else{
					servletGet+=strargs[i][0]+"="+ URLEncoder.encode(strargs[i][1])+"&";
				//}
			}

		   /*  + "Action=" + URLEncoder.encode(Action) + "&"
			 + "Exit=" + URLEncoder.encode(exitStatus) + "&"
			 + "DocID=" + URLEncoder.encode(DocID);*/
			// connect to the servlet
			URL editorHandler = new URL( servletGet );
			servletConnection = editorHandler.openConnection();
		}catch(Exception e){e.printStackTrace();}
		return servletConnection;
	}

The function could be called as.
String params[][]={{"Action","3"           },
                   {"Exit"  ,exitStatus    },
                   {"DocID" ,""+document.getdocID()} };
URLConnection conn;
conn=getServletConnection(params);

-----Original Message-----
From: Haroon Ghaus [mailto:haroon.ghaus@ascotdrummond.co.uk]
Sent: Monday, August 13, 2001 7:56 AM
To: tomcat-dev@jakarta.apache.org
Subject: Applet to Servlet


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 3.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