This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
waiting....
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks,
that was very helpful
 
    Bookmark Topic Watch Topic
  • New Topic