0

I wrote code to get part data from a Servlet request for file upload, but the part list returns zero size. I use JBoss Weld in the JEE project.

But if I remove the Weld configuration from the web.xml file, then it's working fine.

The web.xml configuration is:

<listener>
    <listener-class>
        org.jboss.weld.environment.servlet.Listener</listener-class>
    </listener>
    <resource-env-ref>
        <description>
            Object factory for the CDI Bean Manager
        </description>
        <resource-env-ref-name>BeanManager</resource-env-ref-name>
        <resource-env-ref-type>
            javax.enterprise.inject.spi.BeanManager
        </resource-env-ref-type>
    </resource-env-ref>

The server-side code in the Servlet:

@WebServlet("/upload")  
@MultipartConfig

public class UploadServlet extends HttpServlet {  
    protected void doPost(HttpServletRequest request,
                           HttpServletResponse response)
                             throws ServletException, IOException {
        Collection<Part> parts = request.getParts();
        System.out.println(parts.size());
    }
}

The client-side JavaScript:

$('#input-file').change(function(e) {
    var obj = {
        "name" : "samik"
    };
    var file = e.target.files[0];
    var formData = new FormData();
    formData.append('file', file);
    formData.append('other', JSON.stringify(obj));

    $.ajax({
        url : "upload",
        type : "POST",
        data : formData,
        contentType : false,
        processData : false,
        dataType : "json",
        success : function() { }
    });
});
Sean Mickey
  • 7,618
  • 2
  • 32
  • 58
samik
  • 1
  • 1
  • Some questions: are you sure there is a valid value in the member: `e.target.files[0]`? The `@MultipartConfig` annotation indicates that the servlet expects requests to be made using the `multipart/form-data` MIME type; have you tried calling `request.getHeaders` and seeing what the content type is for the requests sent by your JavaScript? You mentioned that if you remove _Weld_, things work correctly; what does your Servlet code look like in that case? – Sean Mickey Feb 02 '14 at 22:45
  • yes e.target.files[0] gives the correct data. request headers are same in both case. Servlet code are same for both case – samik Feb 03 '14 at 17:28
  • What is in your _/WEB-INF/lib_ directory? Are there errors in the Tomcat log? – Sean Mickey Feb 03 '14 at 18:19
  • no there is not tomcat log error... Watching the log from console... lib folder contains required jar files. – samik Feb 05 '14 at 07:24

1 Answers1

0

The <listener> and the <resource-env-ref> entries in your web.xml appear to be correct; however, because the Tomcat JNDI is read-only, there is an additional configuration you must include. Assuming that you are deploying a WAR to Tomcat, you must include a file named: context.xml in the META-INF directory within your WAR that contains the following entry:

<Context>
    <Resource name="BeanManager" auth="Container"
        type="javax.enterprise.inject.spi.BeanManager"
            factory="org.jboss.weld.resources.ManagerObjectFactory" />
</Context>

Without this entry in the META-INF/context.xml included in your WAR, JBoss Weld will be unable to automatically bind the BeanManager extension into JNDI.

Sean Mickey
  • 7,618
  • 2
  • 32
  • 58
  • i do that.but its not working. Weld is working fine without context.xml.But its disable the working of form data upload. – samik Feb 02 '14 at 17:42
  • only problem with multipart formdata upload.i write a servlet(3.0) for file upload with annotate @MultipartConfig.but multipart data rejected by weld – samik Feb 02 '14 at 17:50
  • @samik Well, it doesn't seem to make sense that Weld would impact a data upload request, so I thought you may have a configuration problem that caused a failure that led to other failures. How do you know that Weld is working fine without the context.xml? Are there any errors, warnings, or anything that looks unusual in the Tomcat logs? – Sean Mickey Feb 02 '14 at 17:52
  • @samik Okay, just saw your latest comment. I suggest that you add the related section of your Servlet code to your question; that will allow me or others to review the complete picture and help you. – Sean Mickey Feb 02 '14 at 17:55
  • @samik The edits you are trying to make to my answer and add as comments to my answer should be made as edits to your original question. You want to add more information to your original question, not edit the answer or add them as comments. – Sean Mickey Feb 02 '14 at 19:01