Search code examples
javajavascriptajaxjsphttpsession

Set session attribute using javascript


I'm using jqueryui autocomplete combobox in my jsp page. I need to set selected value of combo-box to the HttpSession.

I tried as below.

this._on(this.input, {
    autocompleteselect: function (event, ui) {
        // alert(ui.item.value);
        var value = ui.item.value;
        <% session.setAttribute("comboboxvalue",value); %>  

        ui.item.option.selected = true;
        this._trigger("select", event, {
            item: ui.item.option
        });
}

Problem with this way is that code don't recognize value param.

How can I solve this and set session attribute using javascript ?


Solution

  • Here is how I achieved my task successfully.

    I have created a new servlet and trigger an AJAX call as below. Then I set comboboxvalue to session from servlet.

    this._on( this.input, {
        autocompleteselect: function( event, ui ) {
            $.ajax({
            type: 'POST',
            url: 'AjaxServlet',
            dataType : 'json',
            data: { comboboxvalue : ui.item.value  }
            });
            ui.item.option.selected = true;
            this._trigger( "select", event, {
                item: ui.item.option
            });
        },
    
        autocompletechange: "_removeIfInvalid"
    });