Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
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 Murach's Java Servlets and JSP this week in the Servlets 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

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
i would be very grateful....
this is the code

import java.applet.*;
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);

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

// 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

catch(Exception ae){}
this is the code,

import javax.servlet.http.*;
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;

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

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

PrintWriter pwr=response.getWriter();
// 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 nain
Ranch Hand

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

Joined: Mar 22, 2000
Posts: 12759
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:
That 405 error would appear to be a mis-match between the requested output and the way you set the resonse.
chetan nain
Ranch Hand

Joined: Jun 21, 2000
Posts: 159
that was very helpful
I agree. Here's the link:
subject: applet-servlet comm using obj serialization --- william,thomas, maha please help
Similar Threads
applet servlet problem
Servlet could not send Objects to an Applet
Post to Servlet from Applet doesn't work
EOF error
Send object from applet to servlet