0

I am trying send an bitmap and some string fields to server, and I have checked thoroughly no parameter is empty. This is my ajax code

var URL = "ProfileUpdater";

var username = $("#cp-setup-username").val();
var email = $("#cp-setup-email").val();
var birthday = $("#cp-setup-birthday").val();
var aboutYou = $("#cp-setup-about-you").val();    
var intrests = $("#cp-setup-intrests").val();
var phoneNumber = $("#cp-setup-phone-number").val();
var sessionid = $.cookie('WhitePages_SessionID');
var session_username = $.cookie('WhitePages_Username');


var legal = emptyCheck(username,email,birthday,aboutYou,intrests,phoneNumber);
alert(username +" " + email + "  " + birthday +" " + aboutYou + "   "  +intrests + "  " + phoneNumber  +"   "+ sessionid +"  " + session_username);

alert(result);

if(legal){
    alert(sessionid + "  " + session_username) ;     
    alert("about call server");
     $.ajax({
        type:'POST',
        url : URL,
        processData:'false',
        contentType:'false',

        data:{Username:username,Email:email,Birthday:birthday,AboutYou:aboutYou,Intrests:intrests,PhoneNumber:phoneNumber,sessionid:sessionid,username:session_username,Image:result,FieldsChanged:fieldsChanged,isMultiPart:isMultiPart},


        success:function(data){
            alert("success");
            alert(data);
        }
 });

}

and this is my servlet code:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ProfileUpdater extends HttpServlet {



    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


             String user = request.getParameter("Username");
              String sessionid  = request.getParameter("sessionid");
              String username = request.getParameter("username");

               //sessionid and username is null

              PrintWriter out = response.getWriter();
              out.println(sessionid +"   " + username +    "  " + user);
              System.out.println("hello  " + username + "your session id is " +sessionid + "       " + user );
    }



}

I have put alerts in my script and they are not empty but on response it's null. I mean I think it's sending null to server. Can someone please tell me where I am going wrong ??? Thankyou.

KeyStroke
  • 73
  • 1
  • 9

1 Answers1

0

You can't use getParameter() with a multipart post. It just returns null as you discovered. Depending on which servlet spec you are coding to, you can either use the @MultipartConfig annotation (spec 3.0+), or the Apache Commons FileUpload library. The annotation method is built-in, but requires a bit more work; the FileUpload library is easier to use IMO, but obviously requires including another library. I'm not sure which is more efficient in terms of resources or speed. Interestingly if you're using Tomcat it uses FileUpload to handle things in the background anyway.

Mark Olsson
  • 320
  • 2
  • 8