Search code examples
javajavascriptservletsextjs4

Passing form Data to servlet using Extjs


i'm new in ExtJS and Servlet.

I created a form with 2 fields usinf ExtJS :

var panel = Ext.create('Ext.form.Panel', 
{
    title: 'Personnal Data', 
    bodyPainting: 5, 
    width: 350, 
    region:'center',    
    url: 'save_form.php',

    items: 
    [{
        xtype: 'textfield', 
        fieldLabel: 'First Name ',
        name: 'firstName'
    },

    {
        xtype: 'textfield',
        fieldLabel: 'Last Name ',
        name: 'lastName'
    }], 

    buttons:
    [{
        text: 'Submit',  
        handler: function() 
        {
              var formData = this.up('form').getForm();         

        }
    }]
});

But i don't know how to pass the value of the two fields to a servlet, by clicking on the button. And how can i retrieve the data entered in the form, in the servlet ?

Thank you !


Solution

  • By default ExtJS Forms will send over the values in an "ajax" fashion and I believe it will be a "post". In your servlet (assuming you are posting to a servlet defined in your web.xml file) in your doPost (or doGet) methods, use the "request" variable in the method and do

     request.getParameter("firstName")
    

    and

    request.getParameter("lastName")
    

    .

    Link to HttpServlet Definition (is the same for just about every java container).

    Edit:

    In your handler you need to add:

    formData.submit();