Search code examples
gwtgwt2formpanel

formpanel.submit does not submit the file on GWT server


i want to send a file from client to server.

My code:

Client side:

private FormPanel getFormPanel() {
    if (formPanel == null) {
        formPanel = new FormPanel();
    formPanel.setMethod(FormPanel.METHOD_POST);
        formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
        formPanel.setAction(GWT.getHostPageBaseURL() +"UploadFileServlet");
        formPanel.setWidget(getFlexTable_1());

                System.out.println(GWT.getHostPageBaseURL() +"UploadFileServlet");
    }
    return formPanel;
}

In getFlexTable_1()

flexTable.setWidget(1, 1, getFileUpload());

In getFileUpload()

private FileUpload getFileUpload() {
    if (fileUpload == null) {
        fileUpload = new FileUpload();
        fileUpload.setName("upload");
    }
    return fileUpload;
}

private Button getAddButton() {
        if (addButton == null) {
            addButton = new Button("ADD");
            addButton.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                               formPanel.submit();
        }
        });
    }
return addButton;

}

On server side

public class CmisFileUpload extends HttpServlet implements Servlet{

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        byte[] buffer = new byte[115200];//
        String fileName = null;
        String mimetype = null;
        String majorVersion = null;
        InputStream stream = null;
        System.out.println("ServletWorking Fine");
}

Now when i Choose a file and click on ADD button i cant see the output on server side for this code System.out.println("ServletWorking Fine");

The outPut of System.out.println(GWT.getHostPageBaseURL() +"UploadFileServlet"); on client side is

http://127.0.0.1:8888/UploadFileServlet

and when i use this url directly on browser i get server side output for System.out.println("ServletWorking Fine");**


Edited

I created one more web application for file upload

public class Uploadfile implements EntryPoint {

    FormPanel uploadForm = new FormPanel();
    public void onModuleLoad() {

        HorizontalPanel horizontalPanel = new HorizontalPanel();

      uploadForm.setAction(GWT.getHostPageBaseURL() +"UploadFileServlet"); 

        uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART); 
        uploadForm.setMethod(FormPanel.METHOD_POST); 
        horizontalPanel.add(uploadForm);

        // Create a panel to hold all of the form widgets. 
        VerticalPanel panel = new VerticalPanel(); 
        uploadForm.setWidget(panel); 

        FlexTable flexTable = new FlexTable();
        panel.add(flexTable);

        // Create a FileUpload widget. 
        FileUpload upload = new FileUpload(); 
        upload.setName("uploadFormElement"); 
        flexTable.setWidget(2, 3, upload);
        // panel.add(upload); 

        // Add a 'submit' button. 
        Button uploadSubmitButton = new Button("Submit"); 
        panel.add(uploadSubmitButton); 

        uploadSubmitButton.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                // TODO Auto-generated method stub
                 uploadForm.submit(); 
            }
        });
        uploadForm.addFormHandler(new FormHandler() { 
          public void onSubmit(FormSubmitEvent event) { 
          } 
          public void onSubmitComplete(FormSubmitCompleteEvent event) { 
            Window.alert(event.getResults()); 
          } 
        }); 
        RootPanel.get().add(horizontalPanel); 
    }
}

Server

protected void doGet(HttpServletRequest request, 
            HttpServletResponse response) 
                            throws ServletException, IOException { 
      doPost(request, response);
  }

  protected void doPost(HttpServletRequest request, 
            HttpServletResponse response) 
                            throws ServletException, IOException { 

      System.out.println("working fine" );
   }

This code is working fine

According to me there is no difference between the codes.

Plz tell me why the formpanel.submit is not working properly.

Plz help.


Solution

  • Is hide() method is closing the window??? If Yes then

    remove move code hide(); after formPanel.submit();

    for hide() use FormHandler. for eg

    uploadForm.addFormHandler(new FormHandler() {
    
        public void onSubmitComplete(FormSubmitCompleteEvent event) {
            hide();
        }
    
        public void onSubmit(FormSubmitEvent event) {
    
        }
    
    });
    

    reason: The FormPanel must not be detached (i.e. removed from its parent until the submission is complete. Otherwise, notification of submission will fail.