Click to See Complete Forum and Search --> : Applet-Servlet source code


simba
October 13th, 1999, 05:35 PM
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 jkdaniel7@hotmail.com
Thanks

soumyamitra
October 15th, 1999, 01:28 PM
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 soumya@email.com

/* 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("jdbc:odbc: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) { }
}});
}

}