org.cafesip.gwtcomp.server
Class FileUploadServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by org.cafesip.gwtcomp.server.FileUploadServlet
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class FileUploadServlet
extends javax.servlet.http.HttpServlet

This servlet provides you a convenient way to handle HTML forms containing file uploads along with standard input elements like text field, text area, checkbox, etc. It is quite common for HTML forms to contain a file upload widget that allows users to upload a file to the server. For example, a music upload site may use such a widget to enable the user to upload music files along with a title of the song, description, etc. GWT provides a class called FileUpload that helps you place such widgets on your GWT application but it does not provide any utilities on the server side. In fact, you lose the convenience of GWT RPC when a file upload needs widget needs to be placed. This class is an attempt to cover such deficiencies.

Once this servlet is added to the server-side deployment, it can work with the GWT client-side FileUpload class to receive HTML forms with one or more file uploads. This class copies the uploaded files to a temporary directory and invoke methods on application-defined "action" classes to notify the receipt of the request. The "action" class can provide the business logic to handle such request. The action class is shielded from any servlet details. As a part of the servlet init params, you can register an action class. An action class must implement the FileUploadAction interface. See FileUploadAction for details.

To add the servlet to the deployment, you will need to include the following jar files to the WEB-INF/lib directory of your web archive.

All of these files are bundled with the gwtcomp distribution.

In addition, to WEB-INF/web.xml file add a segment similar to the following for every action class that you may have:

      <!-- Change the servlet name and mapping with appropriate name and URL pattern respectively -->
      <servlet>
           <servlet-name>MusicUploadService</servlet-name>
           <servlet-class>
               org.cafesip.gwtcomp.server.FileUploadServlet
           </servlet-class>
           
           <init-param>
               <param-name>action-class</param-name>
               
               <!-- Replace the action class name below with the fully qualified name of the action class -->
               <param-value>mypackage.MyActionClass</param-value>
           </init-param>
       </servlet>
       ...
       <servlet-mapping>
           <servlet-name>MusicUploadService</servlet-name>
           <url-pattern>/myservices/musicUploadService</url-pattern>
       </servlet-mapping>
 
To invoke this service from the client side of the application, you will have to do something like the following:
 public class MusicUploadForm extends FormPanel
 {
 
     private static final String MUSIC_UPLOAD_ACTION = "/myservices/musicUploadServiceService";
 
     public MusicUploadForm() {
      setAction(MUSIC_UPLOAD_ACTION); // set the action, must match with the servlet URL pattern
      setEncoding(FormPanel.ENCODING_MULTIPART);
      setMethod(FormPanel.METHOD_POST);
    
      VerticalPanel panel = new VerticalPanel();
      setWidget(vp);
    
      panel.add(new Label("Song name"));
      TextBox name = new TextBox();
      panel.add(name);
    
      panel.add(new Label("File"));
      FileUpload upload = new FileUpload();
      upload.setName("musicFile");
      panel.add(upload);
    
      Button submit = new Button("Submit");
      submit.addClickListener(new ClickListener()
      {
        public void onClick(Widget sender) {
        submit(); // submit the form
      }
      });
      panel.add(submit)  
    }
 }
 
In the above example, the action class must have an attribute called name with corresponding getter/setter for the FileUploadServlet to populate the name field entered in the form.

See Also:
Serialized Form

Constructor Summary
FileUploadServlet()
          A constructor for this class.
 
Method Summary
protected  void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
 void init()
           
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUploadServlet

public FileUploadServlet()
A constructor for this class.

Method Detail

doPost

protected void doPost(javax.servlet.http.HttpServletRequest request,
                      javax.servlet.http.HttpServletResponse response)
               throws javax.servlet.ServletException,
                      java.io.IOException
Overrides:
doPost in class javax.servlet.http.HttpServlet
Throws:
javax.servlet.ServletException
java.io.IOException

init

public void init()
          throws javax.servlet.ServletException
Overrides:
init in class javax.servlet.GenericServlet
Throws:
javax.servlet.ServletException


http://www.cafesip.org