import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.ArrayList;
import java.util.Vector;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JApplet;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
public class SampleApplet extends JApplet implements ActionListener,Serializable {
private static final long serialVersionUID = 1L;
//String[] table_names = { "one", "two", "three", "four" };
String[] table_names;
String[] col1_names = { "one1", "two1", "three1", "four1" };
String[] col2_names = { "one2", "two2", "three2", "four2" };
ArrayList tnamelist;
private JComboBox box;
private Container contentPane;
private JPanel panel = null;
private JList list;
//@Override
public void init() {
send();
recieve();
}
public void send()
{
try
{
URL url = new URL(getCodeBase(), "http://localhost:8086/prashanthi/DBServlet");
// open a connection to the servlet
URLConnection servletConnection = url.openConnection();
// prepare for both input and output
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);
// don't use a cached version of URL connection
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches (false);
// set the content type to indicate that we're sending binary data
servletConnection.setRequestProperty ("Content-Type", "application/octet-stream");
servletConnection.connect();
ObjectOutputStream outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream());
Vector v=new Vector();
v.addElement("First string from Applet to Servlet");
v.addElement("Second string from Applet to Servlet");
outputToServlet.writeObject(v);
outputToServlet.flush();
outputToServlet.close();
}
catch (Exception ex)
{
System.out.println(ex);
}
}
public void recieve()
{
try
{
URL servletURL = new URL("http://localhost:8086/prashanthi/DBServlet");
URLConnection servletConnection = servletURL.openConnection();
servletConnection.setDoInput(true);
servletConnection.setDoOutput(false);
servletConnection.setUseCaches (false);
servletConnection.setRequestProperty("Content-Type","application/octet-stream");
servletConnection.connect();
ObjectInputStream inputFromServlet=new ObjectInputStream(servletConnection.getInputStream());
ArrayList a=(ArrayList)inputFromServlet.readObject();
table_names=(String[])a.toArray(new String[tnamelist.size()]);
box = new JComboBox(table_names);
box.addActionListener(this);
contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(box, BorderLayout.NORTH);
//textField.setText(v.elementAt(0).toString());
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if(list!=null)
{
contentPane.remove(list);
}
String item = (String) box.getSelectedItem();
if (item.equals("one")) {
list = new JList(col1_names);
} else if (item.equals("two")) {
list = new JList(col2_names);
}
list.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (panel != null)
contentPane.remove(panel);
panel = new JPanel();
String value = (String) list.getSelectedValue();
JLabel lvalue=new JLabel(value);
if(lvalue!=null)
{
panel.remove(lvalue);
}
panel.add(lvalue);
//if (value.contains("one"))
//panel.add(new JLabel("Clicked One"));
//else
//panel.add(new JLabel("Clicked Second One"));
contentPane.add(panel, BorderLayout.SOUTH);
contentPane.validate();
}
});
System.out.println("SampleApplet.actionPerformed() " + item);
contentPane.add(list, BorderLayout.CENTER);
contentPane.validate();
}
}
and my servlet is:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Vector;
import java.sql.*;
public class DBServlet extends HttpServlet implements Serializable
{
Connection con=null;
Statement st=null;
ResultSet rs=null;
ArrayList tnlist;
String s=null;
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
doPost(req,res);
}
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
tnlist=new ArrayList();
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("hello");
try
{
out.println("hello1");
Class.forName("oracle.jdbc.OracleDriver");
out.println("loading driver");
con=DriverManager.getConnection("jdbc:oracle:thin:@10.2.152.251:1533:VIS","edms45","edms45");
st=con.createStatement();
rs=st.executeQuery("select distinct VIEW_NAME from AJ_REPORT_COLUMNS_TMP");
while(rs.next())
{
s=rs.getString(1);
tnlist.add(rs.getString(1));
out.println(s);
}
try
{
ObjectInputStream inputFromApplet = new ObjectInputStream(req.getInputStream());// <-
System.out.println("Obtained Input Stream");
Vector v=(Vector)inputFromApplet.readObject();
out.println("After readObject()");
inputFromApplet.close();
out.println("First String Obtained from applet is "+v.elementAt(0));
out.println("Second String Obtained from applet is "+v.elementAt(1));
ObjectOutputStream outputToApplet=new ObjectOutputStream(res.getOutputStream());
out.println("Obtained output Stream");
outputToApplet.writeObject(tnlist);
outputToApplet.flush();
outputToApplet.close();
out.println("Stream closed");// LAST LINE
}
catch(Exception e)
{
e.printStackTrace();
}
}
catch (SQLException e)
{
out.println(e);
} catch (ClassNotFoundException ce) {
// TODO Auto-generated catch block
out.println(ce);
}
}
}