0

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();
    }
}
 }
Community
  • 1
  • 1
NewCodeLearner
  • 738
  • 3
  • 14
  • 38
  • Did you bind /fileupload to your servlet in your web.xml? If not, then this is it, you need to create a servlet and a servlet-mapping in your web.xml. Now, if you really want to learn, I would rather use GuiceFilter which will simplify your life. – Guillaume Polet Feb 15 '12 at 13:37

1 Answers1

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

Jama A.
  • 15,680
  • 10
  • 55
  • 88
  • I tried to print a System.out.println("test success"); but i cant see this print on console. – NewCodeLearner Feb 15 '12 at 15:00
  • on the server side.inside the method public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } – NewCodeLearner Feb 15 '12 at 15:30
  • check your web.xml, for example, if you have `/testapp/fileupload` url pattern for your servlet, check your form UI from firebug whether it has also proper url. – Jama A. Feb 15 '12 at 16:37
  • my servlet class is in src.com.example.MyProject.server.FileUpload now please what whould be the mapping. I am using tha exact coding as give in above link. – NewCodeLearner Feb 15 '12 at 17:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/7755/discussion-between-newcodelearner-and-jamshid-asatillayev) – NewCodeLearner Feb 15 '12 at 17:42