How to use the http post in GWT

440 views
Skip to first unread message

shgwh

unread,
Feb 9, 2009, 4:13:28 PM2/9/09
to Google Web Toolkit
So actually i'm trying to upload a file by gwt
The problem is that i don't know what should i do in following
functions:
protected String readContent(HttpServletRequest request)
throws ServletException, IOException {
return RPCServletUtils.readContentAsUtf8(request, true);
}
public String processCall(String payload) throws
SerializationException {
try {
RPCRequest rpcRequest = RPC.decodeRequest(payload, this.getClass
(), this);
return RPC.invokeAndEncodeResponse(this, rpcRequest.getMethod(),
rpcRequest.getParameters(), rpcRequest.getSerializationPolicy
());
} catch (IncompatibleRemoteServiceException ex) {
getServletContext().log(
"An IncompatibleRemoteServiceException was thrown while
processing this call.",
ex);
return RPC.encodeResponseForFailure(null, ex);
}
}

These are the implemtation in RemoteServiceServlet, I cannot override
doPost function as it's final, so i have to override these two
functions
But I tried to just return some simple characters, and it opens a file
download window in my browser...So what should i do actually? I think
i need a sample, as simple as possible.

Thank you.

BTW, this is the first time i send a message in english forum, so i'm
sorry if i make something wrong.

shgwh

unread,
Feb 9, 2009, 6:08:26 PM2/9/09
to Google Web Toolkit
Err, sorry, the reason i wan't to override them but not do the
operation before returning of readContent is that:
It always get an exception will processing...
Than the result of this post will be ever null or a dialog to save
file...I can't understand what's it means??

Adam T

unread,
Feb 10, 2009, 1:32:20 AM2/10/09
to Google Web Toolkit
shgwh,

I'd just use a normal servlet rather that the GWT RPC one for file
upload. Search the web for file upload servlet and you'll get plenty
of examples, on the client side you use the fileupload component in a
form and post the data to yout servlet (for example the last post
here:
http://groups.google.se/group/Google-Web-Toolkit/browse_thread/thread/19ea5c6be6d47848/8a4439de9cd48006?hl=sv&lnk=gst&q=fancy+file+upload#8a4439de9cd48006
which uses some apache commons libraries).

To use the servlet in web mode you'll need to add a "servlet" tag to
your module XML definition, and for web mode you'll need to add an
entry to your servers web.xml file in the normal way.

//Adam

On 9 Feb, 22:13, shgwh <sh...@hotmail.com> wrote:
> So actually i'm trying to upload a file by gwt

Lothar Kimmeringer

unread,
Feb 10, 2009, 4:07:09 AM2/10/09
to Google-We...@googlegroups.com
shgwh schrieb:

> These are the implemtation in RemoteServiceServlet, I cannot override
> doPost function as it's final, so i have to override these two
> functions

I solved it by overwriting service:

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (request.getPathInfo() == null || !request.getPathInfo().endsWith("/fileupload")){
super.service(request, response);
return;
}
}

super.service(...) leads to the call of doPost, doGet, etc. in dependence
of the HTTP-method being used.

So on your client-side you can do the following:

Create the form, that will be used to submit the upload-request:

ServiceDefTarget uploadTarget = (ServiceDefTarget) GWT.create(IWebappServices.class);
uploadTarget.setServiceEntryPoint(GWT.getModuleBaseURL() + "YourServiceName");

final FormPanel form = new FormPanel();
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
form.setAction(uploadTarget.getServiceEntryPoint() + "/fileupload");
uploadFile = new FlowPanel();
form.setWidget(uploadFile);
FileUpload upload = new FileUpload();
upload.setName("fileupload");
uploadFile.add(upload);

As you can see, /fileupload is added to the service entry point.

To be able to pass PathInfo-informations to a servlet you need
to define an additional entry in your web.xml:

<servlet-mapping>
<servlet-name>YourServletName</servlet-name>
<url-pattern>/YourServiceName</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>YourServletName</servlet-name>
<url-pattern>/YourServiceName/*</url-pattern>
</servlet-mapping>

Because you have response directly in your hand, you can send
back responses that are not handled by GWT, so you can send
back a content-type of text/plain and a simple OK or an error-
message that can be retrieved with FormSubmitCompleteEvent.getResult
in your onSubmitComplete-method of your FormHandler-implementation.

> But I tried to just return some simple characters, and it opens a file
> download window in my browser...So what should i do actually? I think
> i need a sample, as simple as possible.

Hope that was simple enough. An additional servlet will work
as well, but I prefer this way, because it keeps things together.


Regards, Lothar

shgwh

unread,
Feb 10, 2009, 4:56:11 AM2/10/09
to Google Web Toolkit

> protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
>     if (request.getPathInfo() == null || !request.getPathInfo().endsWith("/fileupload")){
>         super.service(request, response);
>         return;
>     }
>
> }
>
> super.service(...) leads to the call of doPost, doGet, etc. in dependence
> of the HTTP-method being used.
>
So you means that i call my own function to deal with fileupload?

>
>                 ServiceDefTarget uploadTarget = (ServiceDefTarget) GWT.create(IWebappServices.class);
>                 uploadTarget.setServiceEntryPoint(GWT.getModuleBaseURL() + "YourServiceName");
>                 final FormPanel form = new FormPanel();
>                 form.setEncoding(FormPanel.ENCODING_MULTIPART);
>                 form.setMethod(FormPanel.METHOD_POST);
>                 form.setAction(uploadTarget.getServiceEntryPoint() + "/fileupload");

I set action like this:
form.setAction(GWT.getModuleBaseURL() +"/ServiceName");
Could you tell me what's the different with them?
Thank you.

Lothar Kimmeringer

unread,
Feb 10, 2009, 5:21:03 AM2/10/09
to Google-We...@googlegroups.com
shgwh schrieb:

>
>> protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
>> if (request.getPathInfo() == null || !request.getPathInfo().endsWith("/fileupload")){
>> super.service(request, response);
>> return;
>> }
>>
>> }
>>
>> super.service(...) leads to the call of doPost, doGet, etc. in dependence
>> of the HTTP-method being used.
>>
> So you means that i call my own function to deal with fileupload?

You deal with the file-upload below the if-statement that makes
sure that no-upload-requests are processed the way they should
be (i.e. by the GWT framework).

>
>> ServiceDefTarget uploadTarget = (ServiceDefTarget) GWT.create(IWebappServices.class);
>> uploadTarget.setServiceEntryPoint(GWT.getModuleBaseURL() + "YourServiceName");
>> final FormPanel form = new FormPanel();
>> form.setEncoding(FormPanel.ENCODING_MULTIPART);
>> form.setMethod(FormPanel.METHOD_POST);
>> form.setAction(uploadTarget.getServiceEntryPoint() + "/fileupload");
>
> I set action like this:
> form.setAction(GWT.getModuleBaseURL() +"/ServiceName");
> Could you tell me what's the different with them?

The difference is that I added an additional part to the URL:

Instead of http://server.example.com/path/to/ServiceName
I call http://server.example.com/path/to/ServiceName/fileupload
That's what's checked for in the if-statement at the beginning
of the overwritten service-method. If that is missing the
super-class' service-method is called leading to the processing
of a GWT-RPC-method.

If the test is passed you can start processing the upload-request
be splitting the MultiPart-data into parts as you would do it
in a regular servlet handling upload-requests. I assumed that
you already have this part of implementation so I skipped it.

So the acutal service-method is of course longer.


Regards, Lothar

shgwh

unread,
Feb 10, 2009, 6:02:20 AM2/10/09
to Google Web Toolkit
Yes, I got it.
Thank you very much.
Reply all
Reply to author
Forward
0 new messages