• 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 communication

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
> I am doing an Applet-servlet communication program.
> Applet sends its request to a servlet, and servlet
> sends a GoogleSearchResult object.. I can't
> understand
> why the following line should cause a problem.. This is a line from the applet code.. It recieves the GoogleSearcResult object.. I took care of returning a GoogleSearchResult object on the server when I use the following line in my applet.. I have highlighted the problem causing lines with &&&&&&&
Anyone who has done this type of program for GoogleAPI, please let me
know if it worked for you when you return a GoogleSearchResult object.
> GoogleSearchResult s =
> (GoogleSearchResult)inputFromServlet.readObject();

> My code works fine if I use a String object in the
> above line instead of GoogleSearchResult. There has
> to
> be some reason why this causes an error, and I am
> dying to know that reason.. any help is greatly
> appreciated..
>
> Thanks,
> Chandana
******************************************************************
private URLConnection getServletConnection()
throws MalformedURLException, IOException {

URL urlServlet = new
URL("http://localhost:8080/examples/servlet/WebService"; ;
URLConnection con = urlServlet.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty(
"Content-Type",
"application/x-java-serialized-object");

return con;
}
/**
* Send the inputField data to the servlet and show the result in
the outputField.
*/
private void onSendData() {
try {
// get input data for sending
String input = search.getText();
// send data to the servlet
URLConnection con = getServletConnection();
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(input);
oos.flush();
oos.close();

// receive result from servlet
InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
GoogleSearchResult result = (GoogleSearchResult)
inputFromServlet.readObject();
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

inputFromServlet.close();
instr.close();
// show result
//results.setText(result);
} catch (Exception ex) {
ex.printStackTrace();

}
}
***************************************************************
this is the code in my servlet(WebService.class) class..
try {
InputStream in = request.getInputStream();
ObjectInputStream inputFromApplet = new ObjectInputStream(in);
String queryString = (String) inputFromApplet.readObject();
com.google.soap.search.GoogleSearch s = new
com.google.soap.search.GoogleSearch();
s.setKey(clientKey);
s.setQueryString(queryString);
GoogleSearchResult r = s.doSearch();
response.setContentType("application/x-java-serialized-object");
// read a String-object from applet
// instead of a String-object, you can transmit any
object, which
// is known to the servlet and to the applet

// echo it to the applet
OutputStream outstr = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstr);
oos.writeObject(r);
oos.flush();
oos.close();
}
 
Ranch Hand
Posts: 5390
1
Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what error are you getting ??
 
I can't beleive you just said that. Now I need to calm down with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic