File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem sending data from applet to servlet by object serialization

 
rajeev raj
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic