You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Steve Parker <sc...@cisco.com> on 2000/10/03 21:52:00 UTC

HTTP Tunneling: Can't Make it Work

I am trying to get my applet to talk to my servlet, but have not been
successful.  What do I need to look at or for to determine what has gone
wrong?  Also, are there any tricks that I need to do to get it to work
correctly?  I am using Tomcat 3.1 running on Windows NT 4.0 with IIS and
the isapi_redirect.  I've have included my code for the applet and the
servlet.

Any help would be most appreciated,

Steve

Applet Code here:

import java.awt.*;
import java.applet.*;
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 init() {

       Panel p1 = new Panel();
      p1.setLayout(new FlowLayout(FlowLayout.LEFT));

      p1.add(new Label("Query String:"));

      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);

   taResults.setText("Initialized\n");

      add("Center", taResults);
   }

   public void executeQuery() {

      String qryString = tfQuery.getText();

   taResults.append("Inside executeQuery()\n");

      try {
         /* The line below can be adjusted to your local servlet
position */

         URL url = new
URL("http://scparker_nt_t8k:8080/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("MalformedURLException: " + e.toString() +
"\n");
      } catch(IOException e) {
         taResults.setText("IOException: " + e.toString() + "\n");
      }
   }
   public void actionPerformed(ActionEvent ae) {
      executeQuery();
   }
}

Servlet code here:

//Import Servlet Libraries
import javax.servlet.*;
import javax.servlet.http.*;

//Import Java Libraries
import java.util.*;
import java.sql.*;
import java.io.*;

public class DbServlet extends HttpServlet {

   Connection dbCon;

   public void init() throws ServletException {
// Nothing to do
   }

   public void doGet(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {

      PrintWriter out = res.getWriter();
      res.setContentType("text/html");

      String qry = req.getParameter("qry");
      out.println(qry);
      out.close();
   }

   public void destroy() {
// Nothing to do
   }
}