2012-02-15 9 views
0

서블릿 작동 방법을 찾으려고합니다. 이 코드를 사용하여 내 서블릿을 디자인했습니다.정확히 서블릿 GWT에서 어떻게 작동합니까?

client!

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

및 클릭

formPanel.Sumit()에

;

server!

서버에서이 doPost 메서드가 클라이언트에 의해 호출되는 방법을 이해하십시오.

제출 버튼을 클릭하면 개발 모드에서 "test.doc"를 선택할 수 있습니다.

누군가 도와주세요.

소스 코드 클라이언트.

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(); 
     } 
    }); 

서버

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(); 
    } 
} 
} 
+0

web.xml에서/fileupload를 서블릿에 바인딩 했습니까? 만약 그렇지 않다면, 다음은 web.xml에 서블릿과 서블릿 매핑을 생성해야합니다. 이제, 배우고 싶다면 GuiceFilter를 사용하여 삶을 단순화하십시오. –

답변

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> 

당신은 그것의 경로가 fileUploaderServler URL 패턴과 일치하는 것이다 제출을 클릭하면 그래서, 결과적으로 com.testapp.server.FileUpload.doPost(HttpServletRequest request, HttpServletResponse response);이 실행됩니다.

+0

나는 인쇄를 시도했다 System.out.println ("test success"); 하지만 콘솔에서이 인쇄물을 볼 수 없습니다. – NewCodeLearner

+0

어디에서 썼습니까? 당신은/testapp를/fileupload' URL 패턴 '이있는 경우 가 공공 무효의 doPost (HttpServletRequest 요청, HttpServletResponse 응답) ServletException을, IOException가 throw하는 메서드 side.inside 서버 –

+0

는 { } – NewCodeLearner

관련 문제