• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with servlet->applet communication

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

I have a problem when I try to send some data (ObjectOutputStream) from a servlet to an applet.
This is my servlet code:
*************************************
public class SerializeQuestionsAction extends Action{

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
response.setContentType("application/x-java-serialized-object");
ObjectOutput toApplet=((ObjectOutput) response.getOutputStream());
Vector questions=(new ManageQuestionsService().chooseQuestions());
toApplet.writeObject(questions);
return mapping.findForward("success");
} catch (Exception e) {
return mapping.findForward("login-error");
}
}
}
***********************************
Applet Code:
public void doReceive() {
try {
URL urlServlet = new URL(getCodeBase(), "SerializeQuestions.do");
URLConnection con = urlServlet.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);

ObjectInputStream inFromServlet = new ObjectInputStream(con.getInputStream());
Vector v=(Vector)inFromServlet.readObject();
for(int i=0;i<v.size();i++){
QuestionBean p=((QuestionBean)v.get(i));

}


} catch (Exception ex) {
ex.printStackTrace();
}
}

************************************
When I try to run this an IOException occurs:
"java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8080/project/SerializeQuestions.do
"
Could you help me?What's wrong?

Thanks for your help
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Is the execute method invoked at all? Or, before that, does Struts ever get a hand on the request? Having servlets listen to URLs in otherwise public directories always makes me a bit queasy.
 
Namara Krein
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply!

I don't call SerializeQuestions from struts-config (if that was your question, sorry if not but my english is quite bad)
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HTTP 500 is an internal server error - have you checked your log files for errors?
 
Namara Krein
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I have checked that file and it says:
"java.lang.IllegalStateException: getOutputStream() has already been called for this response"
but this is not sense for me because I don't call that method in any other place.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this:

return mapping.findForward("success");

is the problem. After having accessed the output stream, you can't use Struts anymore to do any further processing, because that will access the output stream as well, which isn't permitted. Why that is the case goes way beyond what can be explained in a forum like this. The bottom line is: for direct applet<-->servlet communication, you need to bypass Struts completely (i.e., you can't use *.do URLs).
[ May 22, 2006: Message edited by: Ulf Dittmer ]
 
Namara Krein
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THANK YOU VERY MUCH!!!You have the reason, I have change the Action Servlet and I don't use *.do for the applet-servlet communication. All have run well.Once more, THANKS!!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic