On 23 juin, 15:29, Petein <[email protected]> wrote:
> hi I have a form which sends a request to a servlet for uploading a
> file. How can i handle the exceptions? THe exceptions are caught in
> the doPost method of the servlet. The form in my app looks like:
>
> form = new FormPanel();
>                 form.setEncoding(FormPanel.ENCODING_MULTIPART);
>                 form.setMethod(FormPanel.METHOD_POST);
>
>                 form.setAction("/myfacerecognitionwa/upload");
>
>                 form.addSubmitHandler(new SubmitHandler(){
>
>                         @Override
>                         public void onSubmit(SubmitEvent event) {
>
>                                 try{
>
>                                 }catch(Exception e){
>                                         Window.alert("Error: " + 
> e.getMessage());
>                                         return;
>                                 }
>
>                                 Window.alert("The file was uploaded!");

Actually, at that point, the browser hasn't yet started sending the
file. SubmitComplete is what you're looking for.

> What can I do? I want to catch the exceptions and show them using
> Window.alert("Error: " + ioe.getMessage());

In SubmitCompleteEvent, you get the <body> innerHTML of the response
from the server. It is expected that you do something with it, such as
parse it as XML or JSON or some other "home made" format (treat it as
plain text is a valid option here too). For example:

public void onSubmitComplete(SubmitCompleteEvent event) {
   String results = event.getResults();
   if (results == null) {
      // this is probably an error in *your* code that can be spotted
quite easily during development; there's little to no need to have
such a check in the final deployed app
   }
   results = results.trim();
   if (results.equals("OK")) {
      Window.alert("File uploaded successfully");
   } else {
      Window.alert("Error: " + results);
   }
}

Your servlet in this example would return "OK" if all went well and
some other explanatory message in case of error.

N.B.: there's no way to get the HTTP status code and/or HTTP headers
back from the response when using FormPanel.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to