dcsimg
Problem sending data from applet to servlet by object serialization
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   rajeev_raj
Posted On:   Wednesday, June 23, 2004 12:03 AM

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.getT   More>>

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("jdbc:odbc: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) {}
}    <<Less
About | Sitemap | Contact