> 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
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