How exactly servlet Work in GWT?

杀马特。学长 韩版系。学妹 提交于 2019-12-13 04:58:45

问题


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:


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.



来源:https://stackoverflow.com/questions/9294016/how-exactly-servlet-work-in-gwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!