How exactly servlet Work in GWT?

2019-09-17 14:40发布

I am try to find out How servlet work. I used this code to design my servlet

client!

formPanel.setAction(GWT.getModuleBaseURL()+"fileupload");

and on click

formPanel.Sumit();

server!

in Server, i didnt Understand how this doPost method will be called by the client.

When i click o submit button , i can "you selected test.doc" in development mode.

Please someone help.

Source Code. Client.

   final FormPanel formPanel = new FormPanel();
    formPanel.addFormHandler(new FormHandler() {

        public void onSubmitComplete(final FormSubmitCompleteEvent event) {
            // TODO Auto-generated method stub
            Window.alert(event.getResults());
        }

        public void onSubmit(final FormSubmitEvent event) {
            // TODO Auto-generated method stub
            event.setCancelled(true);
        }
    });
 final FileUpload upload = new FileUpload();
 formPanel.setMethod(FormPanel.METHOD_POST);
    formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
    formPanel.setAction(GWT.getModuleBaseURL()+"fileupload");
             formPanel.setWidget(upload);

      Button btnAdd = new Button("Add");

        btnAdd.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            GWT.log("you selected " + upload.getFilename(), null);
            formPanel.submit();
        }
    });

Server

    public class FileUpload extends HttpServlet {

public void dopost(HttpServletRequest request,HttpServletResponse response){
    ServletFileUpload upload = new ServletFileUpload();
    System.out.println("pratyush file upload");
    try {
        FileItemIterator iterator = upload.getItemIterator(request);

        while (iterator.hasNext()){
            FileItemStream itemStream = iterator.next();

            String name = itemStream.getFieldName();
            InputStream stream = itemStream.openStream();

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            int len;
            byte[] buffer = new byte[8192];
            while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
                outputStream.write(buffer, 0, len);

            }

            int maxFileSize = 2*(1024*1024); 
               if (outputStream.size() > maxFileSize) { 
                   throw new RuntimeException("File is > than " + maxFileSize);
               }

        }
    } catch (FileUploadException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }catch(Exception e){
        throw new RuntimeException();
    }
}
 }

1条回答
姐就是有狂的资本
2楼-- · 2019-09-17 15:01
form.setMethod(FormPanel.METHOD_POST);  //will generate <form method="post"></form>
form.setAction(GWT.getModuleBaseURL()+"fileupload"); 
// and now <form method="post" action="domain/testapp/fileupload"></form>

So when you click submit its path will match the fileUploaderServler url pattern, consequently com.testapp.server.FileUpload.doPost(HttpServletRequest request, HttpServletResponse response); will be executed.

查看更多
登录 后发表回答