Nithyanand
October 11th, 1999, 06:00 PM
I am sending a serialized object from servlet to applet, I am getting it to the input stream, when I try to pass to the ObjectInputStream constructor, I am getting an Stream corrupted Exception.,and it says object not serialized properly, well I have serialized it on the servlet.
The error occurs in the line below
<javacode>
ObjectInputStream oi = new ObjectInputStream(in)
//where in the inputsream object from the server
</javacode>
DHunter21
October 12th, 1999, 10:41 AM
Two Questions:
1. Does the class file for this object reside on the applet side as well?
2. And if so, are you sure its an updated version?
Nithyanand
October 12th, 1999, 02:37 PM
I have the updated class file on the client machine ie where my applet is running.. Any other suggestions..
DHunter21
October 12th, 1999, 03:18 PM
Do you use the Stream before you assign the ObjectInputStream to it? Do you use it as a Data Stream first (for example) , and then try to reassign the stream?
I've got Object Streams working throught sockets on my system, but without some code, I'm shooting in the dark trying to help.
Nithyanand
October 13th, 1999, 11:44 AM
Sorry for keeping you in dark..
This is my applet code:
<java code>
import java.awt.*;
import java.applet.*;
import java.net.*;
import HttpMessage.*;
import java.io.*;
import java.util.*;
import DayTimeSer.*;
public class Applet1 extends Applet
{
public void init()
{
setLayout(null);
setSize(626,404);
label1.setText("Daytime Applet");
add(label1);
label1.setBackground(java.awt.Color.lightGray);
label1.setForeground(java.awt.Color.blue);
label1.setFont(new Font("Dialog", Font.PLAIN, 40));
label1.setBounds(180,36,288,60);
add(textField1);
textField1.setBounds(204,144,235,29);
add(textField2);
textField2.setBounds(204,192,233,30);
add(textField3);
textField3.setBounds(204,240,233,33);
add(textField4);
textField4.setBounds(204,288,230,31);
button1.setLabel("Refresh");
add(button1);
button1.setBackground(java.awt.Color.lightGray);
button1.setBounds(276,348,109,33);
//}}
//{{REGISTER_LISTENERS
SymAction lSymAction = new SymAction();
button1.addActionListener(lSymAction);
//}}
}
//{{DECLARE_CONTROLS
java.awt.Label label1 = new java.awt.Label();
java.awt.TextField textField2 = new java.awt.TextField();
ObjectInputStream result=null;
public void start(){
refresh();
}
public void refresh(){
textField2.setText(getDateUsingHttpObject());
}
private String getDateUsingHttpObject(){
try{
URL url = new URL("http://PServer21:8080/servlet/DayTimeSer");
HttpMessage msg = new HttpMessage(url);
Properties props = new Properties();
props.put("format","object");
System.out.println("hai"+(msg.sendGetMessage(props)).toString());
InputStream in = msg.sendGetMessage(props);
System.out.println("hai1****");
//erro occurs in the following line
try{
ObjectInputStream result= new ObjectInputStream(in);
}catch(StreamCorruptedException e){
e.printStackTrace();
}
System.out.println("hai1****");
Object obj = result.readObject();
Date date = (Date)obj;
return date.toString();
}
catch(Exception e ){
e.printStackTrace();
return null;
}
}
class SymAction implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
Object object = event.getSource();
if (object == button1)
button1_ActionPerformed(event);
}
}
void button1_ActionPerformed(java.awt.event.ActionEvent event)
{
refresh();
}
}
</javacode>
this is my servletcode
<javacode>
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DayTimeSer extends HttpServlet
{
public Date getDate(){
return new Date();
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
if("Object".equals(req.getParameter("format"))){
ObjectOutputStream out = new ObjectOutputStream(resp.getOutputStream());
out.writeObject(getDate());
}
else{
PrintWriter out = resp.getWriter();
out.println(getDate().toString());
}
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
doGet(req,resp);
}
}
</javacode>
This is httpmessage.java used for the interaction
<javacode>
public class HttpMessage {
URL servlet = null;
String args = null;
/**
* Constructs a new HttpMessage that can be used to communicate with the
* servlet at the specified URL.
*
* @param servlet the server resource (typically a servlet) with which
* to communicate
*/
public HttpMessage(URL servlet) {
this.servlet = servlet;
}
/**
* Performs a GET request to the servlet, with no query string.
*
* @return an InputStream to read the response
* @exception IOException if an I/O error occurs
*/
public InputStream sendGetMessage() throws IOException {
return sendGetMessage(null);
}
/**
* Performs a GET request to the servlet, building
* a query string from the supplied properties list.
*
* @param args the properties list from which to build a query string
* @return an InputStream to read the response
* @exception IOException if an I/O error occurs
*/
public InputStream sendGetMessage(Properties args) throws IOException {
String argString = ""; // default
if (args != null) {
argString = "?" + toEncodedString(args);
}
URL url = new URL(servlet.toExternalForm() + argString);
// Turn off caching
URLConnection con = url.openConnection();
con.setUseCaches(false);
return con.getInputStream();
}
/**
* Performs a POST request to the servlet, with no query string.
*
* @return an InputStream to read the response
* @exception IOException if an I/O error occurs
*/
public InputStream sendPostMessage() throws IOException {
return sendPostMessage(null);
}
/**
* Performs a POST request to the servlet, building
* post data from the supplied properties list.
*
* @param args the properties list from which to build the post data
* @return an InputStream to read the response
* @exception IOException if an I/O error occurs
*/
public InputStream sendPostMessage(Properties args) throws IOException {
String argString = ""; // default
if (args != null) {
argString = toEncodedString(args); // notice no "?"
}
URLConnection con = servlet.openConnection();
// Prepare for both input and output
con.setDoInput(true);
con.setDoOutput(true);
// Turn off caching
con.setUseCaches(false);
// Work around a Netscape bug
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// Write the arguments as post data
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(argString);
out.flush();
out.close();
return con.getInputStream();
}
/**
* Performs a POST request to the servlet, uploading a serialized object.
* <p>
* The servlet can receive the object in its <tt>doPost()</tt> method
* like this:
* <pre>
* ObjectInputStream objin =
* new ObjectInputStream(req.getInputStream());
* Object obj = objin.readObject();
* </pre>
* The type of the uploaded object can be retrieved as the subtype of the
* content type (<tt>java-internal/<i>classname</i></tt>).
*
* @param obj the serializable object to upload
* @return an InputStream to read the response
* @exception IOException if an I/O error occurs
*/
public InputStream sendPostMessage(Serializable obj) throws IOException {
URLConnection con = servlet.openConnection();
// Prepare for both input and output
con.setDoInput(true);
con.setDoOutput(true);
// Turn off caching
con.setUseCaches(false);
// Set the content type to be java-internal/classname
con.setRequestProperty("Content-Type",
"java-internal/" + obj.getClass().getName());
// Write the serialized object as post data
ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream());
out.writeObject(obj);
out.flush();
out.close();
return con.getInputStream();
}
/*
* Converts a properties list to a URL-encoded query string
*/
private String toEncodedString(Properties args) {
StringBuffer buf = new StringBuffer();
Enumeration names = args.propertyNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
String value = args.getProperty(name);
buf.append(URLEncoder.encode(name) + "=" + URLEncoder.encode(value));
if (names.hasMoreElements()) buf.append("&");
}
return buf.toString();
}
}
</javacode>
hope you can now find an answer to the problem
thank u in advance
Nithyanand