Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: servlet applet communication

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default servlet applet communication

    Hello...

    Iam stucked with an exception while trying to communicate between applet and servlet.
    the exception is
    java.io.StreamCorruptedException:..
    here are my files.. please give me a solution for this

    applet is:
    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);
            }
     
     
        }
     
     
     
    }

    please let me know the solution for this..
    thanks in advance
    Last edited by JavaPF; May 21st, 2009 at 03:04 AM. Reason: Please use [code] [/code] tags


Similar Threads

  1. What are the requirements to develop Servlet Application?
    By yousef atya in forum Java Servlet
    Replies: 2
    Last Post: July 28th, 2011, 06:20 PM
  2. Add Jmol applet dynimically in JSF(java server faces)
    By megha in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: May 15th, 2009, 06:16 AM
  3. Designing RLC Circuit using Java Applet
    By syxxpac316 in forum Java Applets
    Replies: 6
    Last Post: May 13th, 2009, 04:26 PM
  4. Replies: 0
    Last Post: February 3rd, 2009, 01:15 AM
  5. Problem of implementing mathematic logic in Java applet
    By AnithaBabu1 in forum Java Applets
    Replies: 0
    Last Post: August 15th, 2008, 11:42 PM