Uploadify plugin doesn't call Java Servlet

11,432

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 %>',";
Share:
11,432
MTPy
Author by

MTPy

JEE,JSP,JSR-168 Portlets,Spring Batch,JPA

Updated on June 05, 2022

Comments

  • MTPy
    MTPy almost 2 years

    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.

  • MTPy
    MTPy about 14 years
    Seems,that 'script' param is really taken into consideration,because if it refers to non-existing servlet, that HTTP Error message is appeared on UI.Actually, i couldn't find this message in plugin sources.See,please, screen in bottom of the post.
  • BalusC
    BalusC about 14 years
    Okay. How did you conclude that the servlet isn't triggered? You literally stated like that in your question. Have you for instance debugged the doPost() method? Even the poor man's way with a System.out.println("Hey, uploadservlet doPost() is invoked!");? Have you for instance read all server logs in the /logs folder?
  • MTPy
    MTPy about 14 years
    I just discovered,with help of sniffer,that there is no POST request at all on click for uploading file,i.e. Servlet isn't triggered. Seems, that problem really in Flash config,so in JS as you said.
  • MTPy
    MTPy about 14 years
    sorry, didn't answer your first question in the comment. I concluded, that Servlet isn't called with help of debug.Earlier, when UI was just form with action=Servlet ,that was ok and Servlet method handleRequest was called OK.method is handleRequset, because Servlet class implements org.springframework.web.HttpRequestHandler;
  • BalusC
    BalusC about 14 years
    Debug the JS code. More can I really not be of assistance. Use Firebug or Venkman JS Debugger.
  • MTPy
    MTPy about 14 years
    Oooppph!!! BalusC, it was really hard work. I stuck and asked my TL to help me. Because, i implemented UI part,that i forgot in my debug activity? about filter in another class. And that filter(java method) got null session ID. So this is JsessionID issue,i should share session's value. here is explanation: request.getattribute.com/2009/09/14/17 I will be working on it...
  • MTPy
    MTPy about 14 years
    You're absolutely correct about jsessionid.So the root cause is that Flash doesn't set cookies.And now i got another problem - Uploadify cant' pass more than one param in 'script', but i think that's another question. This issue is resolved.