0

I first used primefaces FileUpload component and it didn't work. Always gave "HTTP Error". So i thought there is some bug with this component and went to plain old JQuery and tried using uploadify. But still i get the same error. I am using Container Managed Security. Is this the reason for not working properly?

This is my script :-

 $(document).ready(function(){
                       $('#photoInput').uploadify({
                        'script'    : '/Blogger/fileUploadServlet',
                        'uploader'  : './uploadify/uploadify.swf',
                        'cancelImg' : './uploadify/cancel.png',
                        'auto'      : true
                        });

UPDATE

Response Headers:

X-Powered-By Servlet/3.0, JSF/2.0 Server GlassFish v3
Set-Cookie JSESSIONID=a23a36b147ac171f8abbf64406cd; Path=/Blogger
Content-Type text/html;charset=UTF-8
Content-Length 1430 Date Thu, 29 Apr 2010 15:16:12 GMT

Request Headers

Host localhost:8080 User-Agent Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.6) Gecko/20091215 Ubuntu/9.10 (karmic) Firefox/3.5.6 
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language en-us,en;q=0.5 
Accept-Encoding gzip,deflate 
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7 
Keep-Alive 300 
Connection keep-alive 
Cookie username=laala; password=panchal; JSESSIONID=a029f59bed8ba0c22fb27a612bf2; treeForm:tree-hi=treeForm:tree:applications; JSESSIONID=962073f17f3d0ebb37536f789b90 Cache-Control max-age=0**

And this is my servlet which is never executed :-

package Servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

@WebServlet(name = "fileUploadServlet", urlPatterns = {"/fileUploadServlet"})
public class fileUploadServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, FileUploadException {
        PrintWriter out = response.getWriter();
        try {
            System.out.println("Executed!!");
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);

            // Create a factory for disk-based file items
            FileItemFactory factory = new DiskFileItemFactory();

// Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);

// Parse the request
            List /* FileItem */ items = upload.parseRequest(request);

            Iterator e = items.iterator();
            while(e.hasNext()){
                System.out.println(e.next().toString());
            }

        } finally {
            out.close();
        }
    }
}

                      });
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
TCM
  • 16,780
  • 43
  • 156
  • 254
  • Please update your question to include the HTTP response header which returned this page and the HTTP request header which was sent by uploadify. You can use firebug or fiddler to grab the HTTP headers. – BalusC Apr 29 '10 at 12:37
  • Is the request header the one when you upload the file? This doesn't look like the one. BTW: you should really not store username/password in a cookie! Store it in the server side session. – BalusC Apr 29 '10 at 15:31
  • No, I refreshed the page and this is what i got. After uploading the file. How can i get request and response header? – TCM Apr 29 '10 at 15:33
  • The `Net` tab in Firebug (especially XHR subtab). If none is fired, then it simply means that JS code is bogus or not executed at all. To nail that further down, you'll need to run the JS debugger to follow the execution line by line. – BalusC Apr 29 '10 at 15:35
  • Ok! But BalusC, i haven't made any change in any of the javascript file. I simply copy pasted the file from the .zip file that i got from uploadify site. Can it be wrong? – TCM Apr 29 '10 at 15:41
  • All i see in XHR subtab even after uploading is "0 request" and nothing else. – TCM Apr 29 '10 at 15:44
  • I haven't give folder as option. Can it be the problem? – TCM Apr 29 '10 at 16:02

1 Answers1

1

Since you didn't post the HTTP request/response headers, it's a bit shooting in the dark. But a common cause is that the file upload request isn't using the same session while that's required by the webapplication in question. You can easily spot that by a missing cookie in the request header.

To fix this, update the line

'script': '/Blogger/fileUploadServlet',

to

'script': '/Blogger/fileUploadServlet;jsessionid=${pageContext.session.id}',

and retry.

Update: The relative URL might be wrong. What's the absolute URL of the JSF page? What's the absolute URL of the upload servlet? You need to extract the right relative URL from it. Right now you have specified the relative URL with a leading slash / so that it's relative to the domain root, i.e. it would absolutely become http://localhost:8080/Blogger/fileUploadServlet.

You may want to consider to leave this aside and retry with a blank/small playground setup as I've outlined in the "Update" part of this answer and see if it works without all other possibly disturbing factors.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Hi Balusc, i edited as you said. After adding jsessionid i still get the error. I am using Glassfish V3. Do i need to make some setting before i can use Flash with Java? Please help me :( – TCM Apr 29 '10 at 15:31
  • Debug the JS with Firebug. Does it get executed correctly? I'm more interested in request headers sent by uploadify. You can find it in `Net` tab of Firebug. Since you're using JSF, have you ensured that `$('#photoInput')` is the right element ID? It should not be the JSF component ID, but the client ID of the generated HTML component (rightclick page, view source). – BalusC Apr 29 '10 at 15:33
  • Yes BalusC, photoInput is the clientId because i have set prependId=false in h:form. Also the component gets the look and feel of Jquery uploadify. Had the id not been correct, it wouln't have got that look and feel. The headers that i sent you were from Net tab only. I don't see any header once i click on browse button of uploadify and select file. All i get is "HTTP - Error". The headers don't get updated in Net tab. What do i do now? – TCM Apr 29 '10 at 15:38