PDA

View Full Version : Parsing upload response



engram
1 Sep 2010, 6:17 AM
Hi,

is there any support by the GXT framework for parsing an upload response. We are using a FormPanel like this:



formPanel.setAction(GWT.getModuleBaseURL() + "MyServlet");
formPanel.setEncoding(Encoding.MULTIPART);
formPanel.setMethod(Method.POST);
formPanel.addListener(Events.Submit, new Listener<FormEvent>() {
@Override
public void handleEvent(final FormEvent event) {
System.out.println(event.getResultHtml());
}
}


The result from the server (event.getResultHtml()) is, as the name implies, is HTML encoded although we write plain text at the server:



response.setContentType("text/plain");
final PrintWriter out;
try {
out = response.getWriter();
if (!success) {
out.println("success");
} else {
out.println("failure");
}
}


In this example the response would be "<PRE>success</PRE>" respectively "<PRE>failure</PRE>", but not "success" or "failure".

The problem is that more complex responses would lead to quite complex "html documents" that are error-prone to parse. Moreover different browser will generate different html responses so that it is nearly impossible to parse them in general.

Are there any best practices for this problem or are we doing anything wrong?

Regards,
engram

sven
1 Sep 2010, 6:20 AM
Only FireFox wraps it into pre tags if you send the wrong contenttype from the server. What contenttype do you set on your server?

engram
1 Sep 2010, 6:23 AM
We've tested it with FF 3.6 and IE 8 and both wrap it into pre tags. We've set the contenttype to "text/plain".

sven
1 Sep 2010, 6:25 AM
set the contentype to
Content-Type: text/html
and it wont wrap it.

I would return some json string from the servlet as that is easy to parse on the clientside.

sven
1 Sep 2010, 6:25 AM
http://code.google.com/p/google-web-toolkit/issues/detail?id=162

engram
1 Sep 2010, 6:42 AM
Thanks for your help. Using text/html works as expected. We will use json for our responses.

Note: If the response is something like "Hello<World>" the result is:

in IE: "Hello<World>"
in FF: "Hello<World></World>"

This is not really a problem for us, but you have to make sure that you don't use any angle brackets in your response text.

sven
1 Sep 2010, 6:50 AM
Yah,

depending on your exact needs you can also use text/plain as contenttype and remove the pretags again(if they are there).

chalu
1 Sep 2010, 11:05 AM
I am about to do a form upload and I am wondering if my RPC servlet can handle the upload, would I have to set the form's action to another servlet e.g
formPanel.setAction(GWT.getModuleBaseURL() + "MyServlet");
or can a RPC servlet handle file uploads. Hope my question is not funny :)

sven
1 Sep 2010, 11:07 AM
The RPC servlet cannot automatically handle it without any code changes. You should start with an own servlet that directly access the doPost method.

Else you need to do overrides to the rpcservlet and based on the data that is received switch between form post and real rpc request.

chalu
1 Sep 2010, 12:42 PM
Alright, I'll use another servlet, thanks.

chalu
2 Sep 2010, 1:44 AM
This has been resolved here (http://www.sencha.com/forum/showthread.php?104461-How-To-Upload-Images-to-the-Server&p=508130#post508130)