servlet在GWT中不起作用

时间:2012-02-27 13:57:19

标签: java gwt servlets

我正在尝试在GWT中使用servlet。我找到了  错误。

   No file found for: /uploadfile/uploadFileServlet

我想浏览文件并在服务器端发送。

我经历了专家提供的许多解决方案。但我无法找到我的错误

有些人帮助解决它。

客户端

package uploadfile.client;
public class Uploadfile implements EntryPoint {

    @SuppressWarnings("deprecation")
    public void onModuleLoad() {
        // TODO Auto-generated method stub
            final FormPanel uploadForm = new FormPanel(); 

            uploadForm.setAction(GWT.getModuleBaseURL() +"uploadFileServlet"); 

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

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

            // Create a FileUpload widget. 
            FileUpload upload = new FileUpload(); 
            upload.setName("uploadFormElement"); 
            panel.add(upload); 

            // Add a 'submit' button. 
            Button uploadSubmitButton = new Button("Submit"); 
            panel.add(uploadSubmitButton); 
            uploadSubmitButton.addClickListener(new ClickListener() { 
              public void onClick(Widget sender) { 
                uploadForm.submit(); 
              } 
            }); 
            uploadForm.addFormHandler(new FormHandler() { 
              public void onSubmit(FormSubmitEvent event) { 
              } 
              public void onSubmitComplete(FormSubmitCompleteEvent event) { 
                Window.alert(event.getResults()); 
              } 
            }); 
            RootPanel.get().add(uploadForm); 
    }

}

服务器端

package uploadfile.server;


public class UploadFileServlet extends HttpServlet implements Servlet 
{ 
   private static final long serialVersionUID = 8305367618713715640L; 

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

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

    response.setContentType("text/plain"); 
    FileItem uploadItem = getFileItem(request); 
    if (uploadItem == null) { 
            response.getWriter().write("NO-SCRIPT-DATA"); 
            return; 
    } 
    byte[] fileContents = uploadItem.get(); 
    //TODO: add code to process file contents here. We will just print 

    System.out.println(new String(fileContents)); 
    response.getWriter().write(new String(fileContents)); 
  } 

   private FileItem getFileItem(HttpServletRequest request) { 

  FileItemFactory factory = new DiskFileItemFactory(); 
  ServletFileUpload upload = new ServletFileUpload(factory); 
  try { 
      List items = upload.parseRequest(request); 
      Iterator it = items.iterator(); 
      while (it.hasNext()) { 
          FileItem item = (FileItem) it.next(); 
          if (!item.isFormField() 
                  && "uploadFormElement".equals(item.getFieldName())) { 
              return item; 
          } 
      } 
  } catch (FileUploadException e) { 
      return null; 
  } 
 return null; 
  } 
 } 

的web.xml

  <?xml version="1.0" encoding="UTF-8"?>
 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>fileUploaderServlet</servlet-name>
<servlet-class>uploadfile.server.UploadFileServlet</servlet-class>
</servlet>
<!-- Servlets 
Default page to serve -->

<servlet-mapping>
<servlet-name>fileUploaderServlet</servlet-name>
<url-pattern>/uploadFileServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Uploadfile.html</welcome-file>
 </welcome-file-list>
</web-app>

1 个答案:

答案 0 :(得分:1)

我不是GWT开发人员所以可能会有所不同,我认为你的servlet操作URL是错误的。您正在尝试使用路径为/uploadfile/uploadFileServlet的servlet,但您的servlet实际上已映射到URL /uploadFileServlet

还有一件事,如果你要延长Servlet,除非你有任何具体的理由,否则你无需实施HttpServlet