aspose file tools*
The moose likes I/O and Streams and the fly likes Problem sending data from applet to servlet by object serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Problem sending data from applet to servlet by object serialization" Watch "Problem sending data from applet to servlet by object serialization" New topic
Author

Problem sending data from applet to servlet by object serialization

rajeev raj
Greenhorn

Joined: Jun 23, 2004
Posts: 1
Hi! all, i m using object serialization to send in data from my applet to servlet to be finally sent in to my database server. m very able to get data into applet from servlet which had been extracted from database server. m using nearly same code snipets for the two purposes. The code for applet to servlet is comliling with no errors but it showz IOException at runtime. I can't figure out why is it so?my data insertion activity from servlet to database is working fine i have checked. The problem seems to b in the passage of data from applet to servlet. Below are the code snippets for applet n the servlets. Anyone provide me with some clue plz.
Applet code snippet to pass data from applet to servlet:

try
{
String aname=name.getText(); // name is a text box.
System.out.println(aname);
URL url = new URL(getCodeBase(),"http://localhost:9090/examples/servlet/sendDataServ");
URLConnection servletConnection2=url.openConnection();
servletConnection2.setDoInput(true);
servletConnection2.setDoOutput(true);
servletConnection2.setUseCaches (false);
servletConnection2.setDefaultUseCaches (false);
servletConnection2.setRequestProperty("Content-Type","application/x-java-serialized-object");
/*String see=servletConnection2.getContent().toString();
System.out.println(see);*/
OutputStream os=servletConnection2.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(aname);
//System.out.println("gone");
oos.flush();
oos.close();
}
catch(Exception e){
System.out.println("no connection");
}

Code snippet of servlet:

public class sendDataServ extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String aname="";
try
{
InputStream in=req.getInputStream();
ObjectInputStream ois= new ObjectInputStream(in);
aname=ois.readObject().toString();
//System.out.println(aname);
/*if(studentRollNo.equals(""))
{System.out.println("got");
}*/
in.close();
}
catch (IOException ioe)
{System.out.println("error1");
}
catch (ClassNotFoundException cnfe)
{System.out.println("error2");
}
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
PreparedStatement ps= null;

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbcdbc:test");
String sql="insert into try4(name) values (?)";
ps = con.prepareStatement(sql);
ps.setObject(1,aname);
ps.executeUpdate();
} catch (SQLException e) {
throw new
ServletException("Servlet Could not display records.", e);
} catch (ClassNotFoundException e) {
throw new
ServletException("JDBC Driver not found.", e);
} finally {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(con != null) {
con.close();
con = null;
}
} catch (SQLException e) {}
}
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8968
    
    9

It would help to see the stack trace of the exception. There may be another exception which causes the IOException.
The first thing to ask is are you downloading the applet from the same server that has the servlet? If not you will get a security exception.


[How To Ask Questions On JavaRanch]
 
 
subject: Problem sending data from applet to servlet by object serialization