Uploadify plugin doesn't call Java Servlet

sergionni picture sergionni · Feb 16, 2010 · Viewed 11.4k times · Source

i just started using Uploadify flash plugin instead of standard HTML UI.
And met the next problem:

when I click "Upload Files" link,that progress is shown and "completed" status is appeared, but in reality - it didn't happened anything,Java Servlet isn't called from backend.

There is upload servlet and uploading performed next way earlier:

< form enctype="multipart/form-data" method="post" target="uploadFrame"
action="<%= request.getContextPath() %>/uploadFile?portletId=${portletId}&remoteFolder=${remoteFolder}">...

After providing Uploadify plugin, UI now looks like:

plugin part(configuration):

    <script>
...  
         oScript.text+= "$j('#uploadify').uploadify({";
      oScript.text+= "'uploader' : 'kne-portlets/js/lib/uploadify/scripts/uploadify.swf',";
      oScript.text+= "'script'   : '<%= request.getContextPath() %>/uploadFile?portletId=${portletId}&remoteFolder=<%= decodedString %>',";
      oScript.text+= "'cancelImg': 'kne-portlets/js/lib/uploadify/cancel.png',";
      oScript.text+= "'folder'   : '<%= decodedString %>',";
      oScript.text+= "'queueID'  : 'fileQueue',";
      oScript.text+= "'auto'     : false,";
      oScript.text+= "'multi'    : false,";
      //oScript.text+= "'sizeLimit' : 1000";
      oScript.text+= "});";
      oScript.text+= "});"; 
...   
</script>

'scripts' parameter here points to Java Servlet on backend

<%= decodedString %> is folder path, which value is \\file-srv\demo

part for uploading:

<input type="file" name="uploadify" id="uploadify" />
<a href="javascript:$j('#uploadify').uploadifyUpload();">Upload Files</a>

Where is my fault?

'Script' param in plugin config points to Java Servlet on backend and it's done,but Servlet isn't triggered.

error, when 'script' param isn't correct:http://img190.imageshack.us/i/errormm.png/

Thank you for assistance.

Answer

BalusC picture BalusC · Feb 16, 2010

This can have a lot of possible causes (also see the comments I posted).

  • External JS is not loaded.
  • JS code is syntactically/logically invalid.
  • Request URL is invalid.
  • Servlet is not mapped at all.
  • Servlet is mapped on wrong url-pattern.
  • Servlet failed to start/init.

It's hard to naildown the root cause based on the as far given information.

As you mentioned that you didn't see any request been fired in the "Net" tab of FireBug, I think that the JS code is simply syntactically/logically invalid. Rightclick page and doubleverify generated/printed JS code.

Update: I tried to reproduce your problem.

  1. I downloaded jquery.uploadify-v2.1.0 (MIT), extracted it and put the entire contents in the /WebContent/uploadify folder of my (empty) playground web project in Eclipse.

  2. I created a /WebContent/upload.jsp file as follows:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>Uploadify test</title>
            <script src="uploadify/jquery-1.3.2.min.js"></script>
            <script src="uploadify/swfobject.js"></script>
            <script src="uploadify/jquery.uploadify.v2.1.0.min.js"></script>
            <script type="text/javascript">
                $(document).ready(function() {
                    $('#uploadify').uploadify({
                        'uploader': 'uploadify/uploadify.swf',
                        'script': 'uploadServlet',
                        'folder': '/uploads',
                        'cancelImg': 'uploadify/cancel.png'
                    });
                    $('#upload').click(function() {
                        $('#uploadify').uploadifyUpload();
                        return false;
                    });
                });
            </script>
        </head>
        <body>
            <input id="uploadify" type="file">
            <a id="upload" href="#">Upload</a>
        </body>
    </html>
    
  3. I created a com.example.UploadServlet as follows with little help of Apache Commons FileUpload (just placed commons-fileupload-1.2.1.jar and commons-io-1.4.jar in /WEB-INF/lib):

    package com.example;
    
    import java.io.IOException;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;
    
    public class UploadServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
        {
            System.out.println("UploadServlet invoked. Here are all uploaded files: ");
            try {
                List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
                for (FileItem item : items) {
                    if (!item.isFormField()) {
                        System.out.println("Name: " + item.getName());
                        System.out.println("Size: " + item.getSize());
                        System.out.println("Type: " + item.getContentType());
                    }
                }
            } catch (Exception e) {
                throw new ServletException(e);
            }
        }
    }
    
  4. I registered com.example.UploadServlet in web.xml as follows:

    <servlet>
        <servlet-name>uploadServlet</servlet-name>
        <servlet-class>com.example.UploadServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>uploadServlet</servlet-name>
        <url-pattern>/uploadServlet</url-pattern>        
    </servlet-mapping>
    
  5. I deployed the project, started the server, went to http://localhost:8080/playground/upload.jsp, selected a random big file from my downloads folder, clicked the Upload link, see the upload percentage counter growing to 100% and I finally see the following in the stdout:

    UploadServlet invoked. Here are all uploaded files: 
    Name: glassfish-v3-windows.exe
    Size: 50402555
    Type: application/octet-stream
    

I'm sorry to say, I can't reproduce your problem. At least, the above information should help you to get started "freshly". Hope it helps.

Update: as per the comments, the filter expects that it is using the same session. Ok, you can do this fairly easy by changing

'<%= request.getContextPath() %>/uploadFile?portletId=${portletId}&remoteFolder=<%= decodedString %>',";

to

'<%= request.getContextPath() %>/uploadFile;jsessionid=${pageContext.session.id}?portletId=${portletId}&remoteFolder=<%= decodedString %>',";