views:

2204

answers:

2

I'm formulating an Ajax request using prototype in the following method:

function updateServerStep(stepNumber){
    alert("updating server step to " + stepNumber);
    var params = {stepNumber:stepNumber};
    alert(params.stepNumber);
    var request = new Ajax.Request('UpdateStep', {
       method:'Post',
       parameters:params,
       onSuccess: function(transport){
        alert("Step changed to " + stepNumber);
       },
       onFailure: function(transport){
        alert("Failed!");
       }
    });
}//updateServerStep

And I have a servlet which tries to get the parameter That I put in the ajax method, but isn't finding any parameters in the request object. And if I try to get the attribute I've set it's listed as being null.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   HttpSession session = request.getSession(true);
   Integer pageNumber = (Integer)request.getAttribute("stepNumber");

   if (pageNumber != null){
     System.out.println("page number was "+ pageNumber.intValue());
     session.setAttribute("secPayStepNum", pageNumber);
    } else {
     System.out.println("page number was null");
     session.setAttribute("secPayStepNum", new Integer(0));
    } 
}

Am I not instantiating the Ajax request properly or Am I making a mistake in getting the parameters?

EDIT

I've changed the javascript to clear any confusion about the name as follows:

function updateServerStep(stepNumber){
    alert("updating server step to " + stepNumber);
    var params = {step:stepNumber};
    alert(params["step"]);
    var request = new Ajax.Request('UpdateStep', {
       method:'Post',
       parameters: {'step':"1"},
       onSuccess: function(transport){
        alert("Step changed to " + stepNumber);
       },
       onFailure: function(transport){
        alert("Failed!");
       }
    });
}//updateServerStep

The Java side is still not getting any parameters, I've added a loop that prints out all the parameters from request.getAttributeNames(); as follows:

Enumeration names = request.getAttributeNames();

      System.out.println("Enumerating Attributes:");
      while( names.hasMoreElements()){
       System.out.println("[ELEMENT] "+ names.nextElement().toString());


      }

and it does nothing aside from print out the line before the loop.

I've also tried putting a bigger array in the params and that doesnt work either, the loop still doesn't iterate.

I've run the Java end in debug mode and there are no attributes being sent. At least not as far as java can see.

I've also tried changing the paramaters to parameters: "step=1&garbage:'hello world'&foo='bar'" because the Prototype docs say you can do that no attributes server side. I've changed the mode to get and appended it to the url and there are no attributes...

A: 

After briefly checking the prototype documentation, your request looks valid.

I recommend inspecting the content of the AJAX request directly. Use Firebug or an external HTTP sniffer like Charles Proxy to verify that the POST request contains your data.

EDIT

I totally missed it - you're definitely defining the object wrong. You can't reuse an existing symbol as an object property without properly delimiting it.

var params = {"stepNumber":stepNumber};

When you don't do this (let's assume stepNumber == 4) then your params object looks like this

{"4":4}
Peter Bailey
I've looked at it incharlies proxy and I don't see an entry for the parameters....
Omar Kooheji
I've fixed this by changing the key to step, still not getting any parameters....
Omar Kooheji
+1  A: 

Use getParameterNames() not getAttributeNames().

Read: http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletRequest.html

and look at the interface function descriptions for the differences between them.

GreenieMeanie
Thank you. Replaced the line in the java with:Integer pageNumber = (Integer.parseInt(request.getParameter("step")));and it works like a charm.
Omar Kooheji