CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 1999
    Posts
    6

    Applet-Servlet source code

    Hi: Has anybody got working source code example of passing param values (string) between an applet and servlet ?? Also please specify which file needs to be placed in which directory of Javawebserver. Please send it to [email protected]
    Thanks


  2. #2
    Join Date
    Oct 1999
    Location
    CA, USA
    Posts
    9

    Re: Applet-Servlet source code

    I am including a sample program to demonstrate how you can establish communication between an Applet and Servlet.

    1) You have to place Servlet class files in Jvawebserver/public_html directory (assuming you are using jeevs).

    2) You will have to plug in the servlet to the Web Server by using Web Server administration screen. For jeeves, you may have to type

    http://YourWebServerName:9090

    To get into the Web Server administration screen.
    Please note, that 9090 is the default port for Administration. If you have altered it, it will be different.

    3) Create a Database in Access, and create an ODBC profile "MyTable" to print to the Database.

    4) In that Database, create a Table named TelephoneNo according to the Table schema provided in the Servlet source code.

    Good Luck!





    // Author Soumya Mitra
    // E-mail [email protected]

    /* This is the code of a sample Servlet, which receives a parameter from the Applet through Http Get request
    and retrieves the entry from a Database table. The Table schema is:
    TelephoneDirectory
    Empcode varchar 5
    FName varchar 30
    TelephoneNo varchar 20


    NOTE: Before compilation of this code, you must set the classpath to

    set classpath=.;c:\jdk1.2\lib;c:\javawebserverpath\lib\jws.jar

    Assuming you have Jeeves installed in your computer. In otherword, all you need is jws.jar to get it
    compiled successfully.
    */


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


    public class ServletTelephoneDirectory extends HttpServlet {

    // The following method will be invoked as soon as a Get request reaches the Web Browser
    public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException {
    String ecode=req.getParameter("Empcode");
    PrintWriter out=res.getWriter(); // Open a output stream to write back to Applet.
    try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Open a JDBC connectivity
    Connection con=DriverManager.getConnection("jdbcdbc:mytable");
    Statement st=con.createStatement();
    ResultSet rs=st.executeQuery("SELECT FName, TelephoneNo FROM TelephoneDirectory WHERE Empcode = ecode;");
    int reccount = 0;
    while(rs.next()) {
    out.println(rs.getString("FName")+","+ rs.getString("TelephoneNo"));
    reccount++;
    }
    if (reccount == 0) { // It means no record found
    out.println("No such record found");
    }
    rs.close();
    st.close();
    con.close();
    } catch(Exception e) {
    out.println("Error");
    }
    } // doGet
    } // end of class





    /* ------------------------------------------------------------------------------------------------------------------------------ */




    /* This is the code of a sample Applet, which sends the Empcode to the above Servlet through Get request
    and retrieves results returned by the Servlet
    */



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


    public class Telephone extends Applet
    {

    Panel mainpanel;
    TextField tfecode, tfname, tftelephone;
    String str;

    public void init() {
    mainpanel = new Panel();
    tfecode = new TextField("Empcode?");
    tfoutput = new TextField("Output will appear here");
    tfoutput.setEditable(false);
    but = new Button("Search");
    mainpanel.add(tfecode);
    mainpanel.add(but);
    mainpanel.add(tfname);
    mainpanel.add(tftelephone);

    add(mainpanel); // Add the panel to the Applet
    mainpanel.setVisible(true); // make it visible


    // Creating anonymous class for button action handler
    but.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e3) {
    try {
    // Create URL for your Web server
    String url="http://YourServerName:80/servlet/ServletTelephoneDirectory?Empcode="+tfecode.getText();
    URL myurl;
    URLConnection ucon;
    myurl=new URL(url);
    ucon= myurl.openConnection(); // AS soon as you open a connection, the Empcode parameter
    // will be transferred to Servlet.

    // Opening a connection to read stream from the Servlet
    BufferedReader br=new BufferedReader(new InputStreamReader(ucon.getInputStream()));
    while((str= br.readLine())!=null) { // Read Servlet output
    tfoutput.setText(str); // Display it in TextField
    } // while
    } catch(Exception e1) { }
    }});
    }

    }







Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured