Hello,

Here is the complete example of getting informations from apllet,  through the servlet.

Applet :


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

   public void executeQuery() {

      String qryString = tfQuery.getText();

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

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


and the servlet :

//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 {

      try {
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         String dbURL = "jdbc:odbc:projects";
         dbCon = DriverManager.getConnection(dbURL);
      } 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");

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


This example returns the result of the query in a field of the applet.

In my example (first message I sent to you), the result of the query comes in my HTML 
page (in the frame in which was displayed the applet).

Hope ths can help you.

Sylvie.

MONICA GARCIA QUESADA a �crit :

> I am afraid, but could you tell me how can I return data to applet
>
> M�nica Garc�a Quesada
> Mercados y Tecnolog�as
> Grupo Desarrollo Internet
> SOLUZIONA TELECOMUNICACIONES
> SERVICIOS PROFESIONALES DE UNI�N FENOSA
> Infanta Mercedes, 31 -2� Pl.
> 28020 MADRID
> TEL.: 915793000
> FAX 915790708
> E-MAIL: [EMAIL PROTECTED]
> www.soluziona.com
> www.ipt.es
>
> >>> Bo Xu <[EMAIL PROTECTED]> 06/19 5:54  >>>
> MONICA GARCIA QUESADA wrote:
>
> > First really thank you,
> >
> > I am not sure, my problem is than I am triying to use a resultset of a jdbcobdc 
>connection in an applet and I have think make a servlet that make the connection and 
>have a function than return the resultset , but the applet can no timport the class 
>of the servlet ??
> > [...]
>
> Hi:-)  I suggest you use the following way:
>
>   client-side                         Servlet                                        
>      DB
> Java-Applet         ->        doGet/doPost...
>                               (access DB with JDBC, get data)       <->      data...
>                            <-   (return data to Applet)
>
> normally, you can-not/don't-need to:
>  - import Servlet class in Applet
>  - use the method of Servlet in Applet Directly
>
> with your Applet, you can:
> - (ask the Servlet contianer to) invoke doGet/doPost... of Servlet
> - then access the DB with JDBC, and get data
> - then return data to Applet
>
> Bo
> June 19, 2001
>
> ___________________________________________________________________________
> 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

___________________________________________________________________________
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