Problem with Applet Servlet communication

Problem with Applet Servlet communication

Post by Johannes Hoelz » Tue, 25 Sep 2001 19:26:33



Hello,

i have a problem with the Applet Servlet communication. The Applet i
wrote should send a query string to a Servlet witch request the data
from a Orcale DB and send the result back to the Applet. But it don't
work.......and i don't know why.

If anybody can help me.....please do.

Thanks a lot

Johannes

The Applet Code:

package web;

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import com.oreilly.servlet.HttpMessage;

public class Applet2 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);
      add("Center", taResults);
   }

   public void executeQuery() {

      String qryString = tfQuery.getText();
      InputStreamReader in=null;

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

         URL url = new URL("http://localhost:8080/servlet/Servlet2");
         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-form-urlencoded");

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

         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 actionPerformed(ActionEvent ae) {
      executeQuery();
   }

Quote:}

The Servlet Code:

package web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
import java.io.*;

public class Servlet2 extends HttpServlet {

   Connection dbCon;

   public void init() throws ServletException {

      try {
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         String dbURL = "jdbc:odbc:TheUrlOfTheDB";
         dbCon = DriverManager.getConnection(dbURL,"XXXX","XXXXX");
      } catch (Exception e) {
         System.out.println("Database connect failed (init)");
         System.out.println(e.toString());
         return;
      }
   }

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

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

      String qry = req.getParameter("qry");
      //String qry=("select fertnr,arbplz,status from moap where
fertnr=2089");

      try {
         Statement s = dbCon.createStatement();
         ResultSet rs = s.executeQuery(qry);
         while (rs.next()) {
            out.println(rs.getString(1) + " -  " + rs.getString(2));
            out.println(rs.getString(3));
            out.println ("");
         }
      } catch (SQLException e) {
         System.out.println(e.toString());
         return;
      }
      out.println();
      out.close();
   }

   public void destroy() {

      /* Close database connection */
      try {
         dbCon.close();
      } catch (Exception e) {
         System.out.println("Error closing database (destroy)");
         System.out.println(e.toString());
      }
   }

Quote:}

 
 
 

Problem with Applet Servlet communication

Post by Martin Ramsha » Wed, 26 Sep 2001 11:24:43



>Hello,

>i have a problem with the Applet Servlet communication. The Applet i
>wrote should send a query string to a Servlet witch request the data
>from a Orcale DB and send the result back to the Applet. But it don't
>work.......and i don't know why.

<snip>

Quote:>         URL url = new URL("http://localhost:8080/servlet/Servlet2");
>         String qry = URLEncoder.encode("qry") + "="
>+URLEncoder.encode(qryString);

You don't specify what your actual problem is. To narrow it down,
open a browser, and check the above URL (with appropriate
parameters). If your servlet works correctly, you can proceed
 to debug your applet. If not, then you need to debug your servlet.

    My $.02

 
 
 

Problem with Applet Servlet communication

Post by Johannes Hoelz » Wed, 26 Sep 2001 15:36:47




> >Hello,

> >i have a problem with the Applet Servlet communication. The Applet i
> >wrote should send a query string to a Servlet witch request the data
> >from a Orcale DB and send the result back to the Applet. But it don't
> >work.......and i don't know why.

> <snip>

> >         URL url = new URL("http://localhost:8080/servlet/Servlet2");
> >         String qry = URLEncoder.encode("qry") + "="
> >+URLEncoder.encode(qryString);

> You don't specify what your actual problem is. To narrow it down,
> open a browser, and check the above URL (with appropriate
> parameters). If your servlet works correctly, you can proceed
>  to debug your applet. If not, then you need to debug your servlet.

Well...my problme is.....if i start the servlet alone....with an
prepared statement....i got the data i like.....but if i send the
statement (qry) with the applet to the servlet.....nothing
happends.....all works fine when the statemant  is fix in the servlet.
 
 
 

Problem with Applet Servlet communication

Post by Martin Ramsha » Thu, 27 Sep 2001 12:03:54


Quote:>Well...my problme is.....if i start the servlet alone....with an
>prepared statement....i got the data i like.....but if i send the
>statement (qry) with the applet to the servlet.....nothing
>happends.....all works fine when the statemant  is fix in the servlet.

Looks like you're doing a 'post' rather than a 'get'.

Try the following in your applet:

         URL url = new URL("http://localhost:8080/servlet/Servlet2?qry="
                                             +
URLEncoder.encode(qryString) );

Or, you could duplicate your servlet 'get' code as servlet 'post' code.

 
 
 

Problem with Applet Servlet communication

Post by Heikki Mets » Thu, 08 Nov 2001 03:41:52


Hey!!

       uc.setRequestProperty("Content-Type","application/x-form-urlencoded");

you are doing url coding double time.. I thing...
Bellow you find a pease of code from working Applet to servlet communication and
allso a
link to site from wich you can lear more..
Hope this was so help

Heikki

       URL u = new URL( servlet );
        //HttpsURLConnection
        URLConnection con = u.openConnection();
          // supposed to return a HttpURLConnection object, but doesn't

        con.setDoInput( true );
        con.setDoOutput( true );
        con.setUseCaches( false );
        //con..setRequestMethod("POST");
        con.setRequestProperty("Content-type","application/octet-stream");
        con.setAllowUserInteraction( false );

    The original source can be found at <a
href=http://www.javaranch.com>JavaRanch</a> <p>

-------------------------------------


> Hello,

> i have a problem with the Applet Servlet communication. The Applet i
> wrote should send a query string to a Servlet witch request the data
> from a Orcale DB and send the result back to the Applet. But it don't
> work.......and i don't know why.

> If anybody can help me.....please do.

> Thanks a lot

> Johannes

> The Applet Code:

> package web;

> import java.awt.*;
> import java.applet.*;
> import java.awt.event.*;
> import java.io.*;
> import java.net.*;
> import com.oreilly.servlet.HttpMessage;

> public class Applet2 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);
>       add("Center", taResults);
>    }

>    public void executeQuery() {

>       String qryString = tfQuery.getText();
>       InputStreamReader in=null;

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

>          URL url = new URL("http://localhost:8080/servlet/Servlet2");
>          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-form-urlencoded");

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

>          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 actionPerformed(ActionEvent ae) {
>       executeQuery();
>    }
> }

> The Servlet Code:

> package web;

> import javax.servlet.*;
> import javax.servlet.http.*;
> import java.util.*;
> import java.sql.*;
> import java.io.*;

> public class Servlet2 extends HttpServlet {

>    Connection dbCon;

>    public void init() throws ServletException {

>       try {
>          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
>          String dbURL = "jdbc:odbc:TheUrlOfTheDB";
>          dbCon = DriverManager.getConnection(dbURL,"XXXX","XXXXX");
>       } catch (Exception e) {
>          System.out.println("Database connect failed (init)");
>          System.out.println(e.toString());
>          return;
>       }
>    }

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

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

>       String qry = req.getParameter("qry");
>       //String qry=("select fertnr,arbplz,status from moap where
> fertnr=2089");

>       try {
>          Statement s = dbCon.createStatement();
>          ResultSet rs = s.executeQuery(qry);
>          while (rs.next()) {
>             out.println(rs.getString(1) + " -  " + rs.getString(2));
>             out.println(rs.getString(3));
>             out.println ("");
>          }
>       } catch (SQLException e) {
>          System.out.println(e.toString());
>          return;
>       }
>       out.println();
>       out.close();
>    }

>    public void destroy() {

>       /* Close database connection */
>       try {
>          dbCon.close();
>       } catch (Exception e) {
>          System.out.println("Error closing database (destroy)");
>          System.out.println(e.toString());
>       }
>    }
> }