Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Applet to Servlet

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using object streams to communicate between an applet and
servlet and keep getting:
java.io.InvalidClassException: java.util.ArrayList; illegalAccessException
Everything works great if I am returning just a string from
the servlet to the applet. However, when I try it with an
ArrayList filled with Strings it bombs on me. I have tried
using the different content types and all sorts of castings
on the applet side and nothing seems to work. An ArrayList
as well as a String implement serializable so I don't know
what the problem is. Any light you can shed on it would be
greatly appreciated.
Thanks,
Ez

************ Servlet code *******************
import java.util.*;
import java.io.*;
public class GetRecipients extends javax.servlet.http.HttpServlet
{
public void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
{
doPost(request,response);
}
public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
{
System.out.println("In do post");
//response.setContentType("text/html");
//PrintWriter out = response.getWriter();
//out.println("<html><body>In doPost</body></html>");
performTask(request, response);
}
public void init()
{
// insert code to initialize the servlet here
}
public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
{
try
{
//response.setContentType("application/octet-stream");
response.setContentType("java-internal/class" );
ObjectInputStream objIn = new ObjectInputStream(request.getInputStream());
String dataFromApplet = (String)objIn.readObject();
ArrayList al = new ArrayList();
al.add("first element");
al.add("second element");
al.add("third element");
System.out.println("Data from applet is: " + dataFromApplet );

ObjectOutputStream objOut = new ObjectOutputStream(response.getOutputStream());
//objOut.writeObject(new String("data from server"));
objOut.writeObject(al);
//flush it
objOut.flush();
objOut.close();
}
catch(Throwable theException)
{
theException.printStackTrace();
}
}
} // end servlet
******************** applet code ************************
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.ArrayList;
import java.net.*;
import java.io.*;
public class Applet1 extends Applet {
boolean isStandalone = false;
Button button1 = new Button();
ScrollPane scrollPane1 = new ScrollPane();
BorderLayout borderLayout1 = new BorderLayout();
TextArea textArea1 = new TextArea();
// servlet & url communication vars
URL currentPage;
String protocol = "";
String host = "";
int port;
private void loadCommunicationVars()
{
currentPage = getCodeBase();
protocol = currentPage.getProtocol();
host = currentPage.getHost();
port = currentPage.getPort();
}
/**Construct the applet*/
public Applet1() {
}
/**Initialize the applet*/
public void init() {
try {
jbInit();
loadCommunicationVars();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
button1.setLabel("Talk to servlet");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
this.setLayout(borderLayout1);
this.add(button1, BorderLayout.NORTH);
this.add(scrollPane1, BorderLayout.CENTER);
scrollPane1.add(textArea1, null);
}
void button1_actionPerformed(ActionEvent e) {
getServletData();
}
private void getServletData()
{
try
{
// open a connection to the servletURL
URL dataURL = new URL("http://localhost/servlet/GetRecipients");
URLConnection connection = dataURL.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
// Don't use a cached version of URL connection.
connection.setUseCaches (false);
connection.setDefaultUseCaches (false);
// Specify the content type that we will send binary data
//connection.setRequestProperty ("Content-Type", "application/octet-stream");
connection.setRequestProperty ("Content-Type", "java-internal/class" );
// send the servlet our necessary information
ObjectOutputStream out = new ObjectOutputStream(connection.getOutputStream());
out.writeObject("testString");
out.flush();
out.close();

System.out.println("ok sending");

// servlet sends back a message
ObjectInputStream in = new ObjectInputStream(connection.getInputStream());
//String status = (String)in.readObject();
System.out.println("reading object");
Object o = (Object)in.readObject();
System.out.println("object read");
String status = o.getClass().getName();

//ArrayList al = (ArrayList) in.readObject();
System.out.println("class from server is: " + status );
//for( int i=0; i < al.size(); i++)
//{
// textArea1.append("element: " + i + " is" + (String) al.get(i) );
//}

//textArea1.append("class is " + status + "\n");
//System.out.println("Data from server is: " + status);
in.close();
}
catch( ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
catch( MalformedURLException e )
{
e.printStackTrace();
}
catch( IOException ioe )
{
ioe.printStackTrace();
}
}
}
 
Why fit in when you were born to stand out? - Seuss. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic