SubmitCompleteEvent.getResults on File upload

2,336 views
Skip to first unread message

Peter Ondruska

unread,
Dec 25, 2009, 6:50:33 PM12/25/09
to Google Web Toolkit
My upload servlet successfully accepts data however the response sent
is not what I expect:

<pre style="word-wrap: brak-word; white-space: pre-wrap;">OK</pre>

instead of just:

OK

The servlet basically responds with:

response.setContentType("text/plain");
response.getWriter().write("OK");

I have tried with curl to post something and servlet (GAE/J SDK) sends
only "OK" but in GWT application I see *<pre style="word-wrap: brak-
word; white-space: pre-wrap;">OK</pre>* when calling event.getResults
().

Any clue what am I doing wrong?

Andrés Cerezo

unread,
Dec 28, 2009, 4:56:37 AM12/28/09
to google-we...@googlegroups.com
I need more information, can yoyu sen the source code of the program.java ?

Thanks.

2009/12/26 Peter Ondruska <peter.o...@gmail.com>

--

You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.



Peter Ondruska

unread,
Dec 28, 2009, 8:58:59 AM12/28/09
to Google Web Toolkit
public class Upload extends HttpServlet {

private static final long serialVersionUID = -7859156850837921885L;
private final Logger logger = Logger.getLogger(getClass().getName());

@SuppressWarnings("unchecked")
@Override
protected void doPost(final HttpServletRequest request, final
HttpServletResponse response) throws ServletException,
IOException {

response.setContentType("text/plain");

if (!ServletFileUpload.isMultipartContent(request))
return;

final PersistenceManager pm = PMF.get().getPersistenceManager();
final ServletFileUpload upload = new ServletFileUpload();
upload.setSizeMax(1024*1024-256);
try {
final FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
final FileItemStream fis = iterator.next();
if (!fis.isFormField()) {
final String name = fis.getName();
final String mimeType = fis.getContentType();
// TODO overwrite existing if owner matches
final InputStream is = fis.openStream();
final Blob blob = new Blob(IOUtils.toByteArray(is));
final Document document = new Document(name, mimeType, blob);
final UserService us = UserServiceFactory.getUserService();
final User user = us.getCurrentUser();
document.setOwner(user);
pm.makePersistent(document);

Cache cache = null;
try {
CacheFactory cacheFactory = CacheManager.getInstance
().getCacheFactory();
cache = cacheFactory.createCache(Collections.emptyMap());
cache.put("docname~" + name, blob.getBytes());
cache.put("doctype~" + name, mimeType);
} catch (CacheException e) {
logger.log(Level.SEVERE, "failed to configure cache", e);
}

}
}
response.getWriter().write("OK");
} catch (SizeLimitExceededException e) {
response.getWriter().write("Too big");
} catch (FileUploadException e) {
logger.log(Level.SEVERE, null, e);
} finally {
pm.close();
}

}

}


On 28 pro, 10:56, Andrés Cerezo <acerezoguil...@gmail.com> wrote:
> I need more information, can yoyu sen the source code of the program.java ?
>
> Thanks.
>

> 2009/12/26 Peter Ondruska <peter.ondru...@gmail.com>


>
>
>
> > My upload servlet successfully accepts data however the response sent
> > is not what I expect:
>
> > <pre style="word-wrap: brak-word; white-space: pre-wrap;">OK</pre>
>
> > instead of just:
>
> > OK
>
> > The servlet basically responds with:
>
> > response.setContentType("text/plain");
> > response.getWriter().write("OK");
>
> > I have tried with curl to post something and servlet (GAE/J SDK) sends
> > only "OK" but in GWT application I see *<pre style="word-wrap: brak-
> > word; white-space: pre-wrap;">OK</pre>* when calling event.getResults
> > ().
>
> > Any clue what am I doing wrong?
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "Google Web Toolkit" group.
> > To post to this group, send email to google-we...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsubs­cr...@googlegroups.com>


> > .
> > For more options, visit this group at

> >http://groups.google.com/group/google-web-toolkit?hl=en.– Skrýt citovaný text –
>
> – Zobrazit citovaný text –

mably

unread,
Jan 26, 2010, 6:43:36 PM1/26/10
to Google Web Toolkit
A solution to this problem anyone ?

On 28 déc 2009, 14:58, Peter Ondruska <peter.ondru...@gmail.com>
wrote:

Thomas Broyer

unread,
Jan 26, 2010, 7:38:26 PM1/26/10
to Google Web Toolkit

On 27 jan, 00:43, mably <fm2...@mably.com> wrote:
> A solution to this problem anyone ?

The JavaDoc says "send back text/html". Follow this advice, really
(just pay attention to "<" and "&" chars that might appear in your
output).

> On 28 déc 2009, 14:58, Peter Ondruska wrote:
>
> >                 response.setContentType("text/plain");

Peter Ondruska

unread,
Jan 27, 2010, 3:12:24 AM1/27/10
to Google Web Toolkit
Thanks Thomas, for me it was not obvious:
http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/user/client/ui/FormSubmitCompleteEvent.html#getResults()

"Returns: the result html, or null if there was an error reading it"
perhaps it would be more clear to state "Returns: HTML encoded result
regardless of content type returned by servlet..."

> > >                 response.setContentType("text/plain");– Skrýt citovaný text –
>
> – Zobrazit citovaný text –

Thomas Broyer

unread,
Jan 27, 2010, 6:18:47 AM1/27/10
to Google Web Toolkit

On Jan 27, 9:12 am, Peter Ondruska <peter.ondru...@gmail.com> wrote:
> Thanks Thomas, for me it was not obvious:http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/g...()


>
> "Returns: the result html, or null if there was an error reading it"
> perhaps it would be more clear to state "Returns: HTML encoded result
> regardless of content type returned by servlet..."

FormPanel submits to a hidden iframe, so it gets the result from the
iframe's content (hence the 'null' when you don't submit to the same
domain, because of the SOP), which is always seen as an HTML document,
even if it "represents" a text/plain or an image.

The hint is in FormPanel's no-arg constructor's javadoc:
http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/user/client/ui/FormPanel.html#FormPanel()
"""
The back-end server is expected to respond with a content-type of
'text/html', meaning that the text returned will be treated as HTML.
If any other content-type is specified by the server, then the result
HTML sent in the onFormSubmit event will be unpredictable across
browsers, and the onSubmitComplete event may not fire at all.

Tip:
The initial implementation of FormPanel specified that the server
respond with a content-type of 'text/plain'. This has been
intentionally changed to specify 'text/html' because 'text/plain'
cannot be made to work properly on all browsers.
"""

mably

unread,
Jan 27, 2010, 8:40:26 AM1/27/10
to Google Web Toolkit
It works, thanx for your answer.

François

Peter Ondruska

unread,
Jan 28, 2010, 8:09:33 AM1/28/10
to Google Web Toolkit
Thank you again Thomas, that makes sense now :-) Peter

On 27 led, 12:18, Thomas Broyer <t.bro...@gmail.com> wrote:
> On Jan 27, 9:12 am, Peter Ondruska <peter.ondru...@gmail.com> wrote:
>
> > Thanks Thomas, for me it was not obvious:http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/g...()
>
> > "Returns: the result html, or null if there was an error reading it"
> > perhaps it would be more clear to state "Returns: HTML encoded result
> > regardless of content type returned by servlet..."
>
> FormPanel submits to a hidden iframe, so it gets the result from the
> iframe's content (hence the 'null' when you don't submit to the same
> domain, because of the SOP), which is always seen as an HTML document,
> even if it "represents" a text/plain or an image.
>

> The hint is in FormPanel's no-arg constructor's javadoc:http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/g...()

aliman

unread,
Feb 3, 2010, 11:58:32 AM2/3/10
to Google Web Toolkit
Additionally, I find that if you return an HTML document as the
response to the form submission, the value of getResults() is the
content of the body element in the HTML document.

I don't know if this is a good idea, but I return an HTML document
like...

<html><head><title>results</title></head><body><!-- [put anything you
want to process on submit complete inside this XML comment] --></
body></html>

... and then process the string I get from getResults(), which will be
"<!-- ... -->".

I actually want to send some XML back as the response to the form
submission, and initially tried putting the XML directly inside the
body element, but that has strange results, and so embed it in an XML
comment, then extract from there and parse.

This is an ugly hack, so any other suggestions very welcome.

In particular, the fact that you cannot get access to the status code
of the response is particularly annoying. It means you have to rely on
parsing the value of getResults() to determine whether the upload
failed or succeeded, which isn't consistent with the good practice of
relying on sensible HTTP status codes.

Cheers,

Alistair

Niki

unread,
Sep 27, 2012, 7:56:30 AM9/27/12
to google-we...@googlegroups.com, alim...@googlemail.com
Thanks,Alistair!

This really helped me!Thanks a lot!
I've made a little modification like this:
<pre><!-- escaped text, to block unexpected end of comment --></pre>
And I recieve exactly this in the client( through event.getResult() ).In some browsers it is <PRE> insted of <pre>,namely IE.
Reply all
Reply to author
Forward
0 new messages