aspose file tools*
The moose likes Servlets and the fly likes applet-servlet comm using obj serialization --- william,thomas, maha please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "applet-servlet comm using obj serialization --- william,thomas, maha please help" Watch "applet-servlet comm using obj serialization --- william,thomas, maha please help" New topic
Author

applet-servlet comm using obj serialization --- william,thomas, maha please help

chetan nain
Ranch Hand

Joined: Jun 21, 2000
Posts: 159
here is the problem,

i want to implement applet-to-servlet communication using the object serialization feature
of java . i have written the following two classes- applettoserlvet and servletfromapplet.
both compile fine, but the problem is that, on clicking the 'submit' button , the new window,
displaying the servlet code does not popup, and when i try to run the servletfromapplet from
the browser, i get a 405-resource not allowed error.
what exactly is going wrong?
please help me out here, i spent the whole night trying to figure out the solution.
william., could you try to compile and run the following programs and tell me the possible
solution?
i would be very grateful....
this is the code applettoservlet.java

import java.applet.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.awt.*;
public class applettoservlet extends Applet implements ActionListener{
//
// Applet client-side code to send a student object
// to a servlet in a serialized fashion.
//
// A POST method is sent to the servlet.
//
String webServerStr = "http://localhost:8080/test/servlet/servletfromapplet";
String theStudent="chetan";
public void init(){
Button b1=new Button("Submit");
TextField t1=new TextField(50);
add(b1);
add(t1);
b1.addActionListener(this);


}
public void actionPerformed(ActionEvent e){
try
{
URL studentDBservlet = new URL( webServerStr );
URLConnection servletConnection = studentDBservlet.openConnection();
// inform the connection that we will send output and accept input
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);

// Don't use a cached version of URL connection.
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches (false);
// Specify the content type that we will send binary data
servletConnection.setRequestProperty ("Content-Type", "application/octet-stream");

// send the student object to the servlet using serialization
ObjectOutputStream outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream());

// serialize the object
outputToServlet.writeObject(theStudent);

outputToServlet.flush();
outputToServlet.close();
}
catch(Exception ae){}
}
}
this is the code, servletfromapplet.java

import javax.servlet.http.*;
import java.io.*;
public class servletfromapplet extends HttpServlet {
// Listing 3
//
// Servlet server-side code to read a serialized
// student object from an applet.
//
// The servlet code handles a POST method
//
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
ObjectInputStream inputFromApplet = null;
String aStudent = null;
PrintWriter out = null;
BufferedReader inTest = null;

try
{
// get an input stream from the applet
inputFromApplet = new ObjectInputStream(request.getInputStream());

// read the serialized student data from applet
aStudent = (String) inputFromApplet.readObject();

inputFromApplet.close();
PrintWriter pwr=response.getWriter();
pwr.write(aStudent);
// continue the process for registering the student object
}
catch(Exception e)
{
// handle exception
}

}
}
also, we have to place the applet and html files in a particuar directory on the tomcat server?
if so, which dir???
anxiously awaiting the solution....
chetan
chetan nain
Ranch Hand

Joined: Jun 21, 2000
Posts: 159
waiting....
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12805
    
    5
I don't have time to debug that but I can point you to working code for serializing objects between servlet and applet. It was created by our host here on javaranch:
http://www.javaranch.com/common.html
That 405 error would appear to be a mis-match between the requested output and the way you set the resonse.
Bill
chetan nain
Ranch Hand

Joined: Jun 21, 2000
Posts: 159
thanks,
that was very helpful
 
wood burning stoves
 
subject: applet-servlet comm using obj serialization --- william,thomas, maha please help