dcsimg
Serializing objects from applet to servlet
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Janusz_Dalecki
Posted On:   Tuesday, March 25, 2003 02:03 AM

I have attached the code below that I have a problem with. This applet is using URLConnection to serialize java object to the receiving servlet. The problem I am having is that if I don't include the part that is reading from BufferedReader, then my servlet is not even invoked. What am I doing wrong? Any help appreciated. Janusz private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) { // Add your handling code here: try { UserDetails ud = userPanel.getUserDetails(); StringBuffer url = new StringBuffer("http://"+applet.getCodeBase().getHost()+":7001/build/CommandServlet" + "?" + URLEncoder.enco   More>>


I have attached the code below that I have a problem with. This applet is using URLConnection to serialize java object to the receiving servlet. The problem I am having is that if I don't include the part that is reading from BufferedReader, then my servlet is not even invoked. What am I doing wrong?
Any help appreciated.
Janusz




			
private void updateButtonActionPerformed(java.awt.event.ActionEvent evt)
{
// Add your handling code here:
try
{
UserDetails ud = userPanel.getUserDetails();
StringBuffer url = new StringBuffer("http://"+applet.getCodeBase().getHost()+":7001/build/CommandServlet"
+ "?"
+ URLEncoder.encode("command")
+ "="
+ URLEncoder.encode("SaveUser")
);
URL locator = new URL(url.toString());
HttpURLConnection c = (HttpURLConnection)locator.openConnection();
System.out.println("Connection="+c.toString());

// Inform the conection that we will send output and accept input
c.setDoInput(true);
c.setDoOutput(true);

// Don't use a cached version of URL connection.
c.setUseCaches(false);
c.setDefaultUseCaches(false);

// Specify the content type that we will send binary data
c.setRequestProperty("Content-Type", "application/octet-stream");

// Send user details to the servlet using serialization
ObjectOutputStream ous = new ObjectOutputStream(c.getOutputStream());

// serialize the object
System.out.println("Prior to serialize");
ous.writeObject(ud);
System.out.println("After to serialize");
ous.flush();
ous.close();

// Read response - that the part that I have to include for the code to work
BufferedReader inFromServlet = null;
try
{
// now, let's read the response from the servlet.
// this is simply a confirmation string
inFromServlet = new BufferedReader(new InputStreamReader(c.getInputStream()));
String str;
while (null != ((str = inFromServlet.readLine())))
{
System.out.println("Reading servlet response: " + str);
}
inFromServlet.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}

   <<Less
About | Sitemap | Contact