Search code examples
jspservletsscriptlet

How to passing parameter on JSP to servlet without link or do clicked


I had problem to refresh my list, I will do send parameter to servlet

when I do with hyperlink I will do like this.

<a href="ServletTransaksi?action=refresh">Refresh table</a>

I use scriptlet. How can I refresh my list without clicked something.

thanks


Solution

  • You can use Javascript, and bind an AJAX request to any page. For example, when the page loads, you can add the code.

    Example

    document.addEventListener("load", function () {
        var xmlhttp;
    
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    // Your request has gone through.
                }
            }
        }
    
        xmlhttp.open("GET", "ServletTransaksi?action=refresh", true);
        xmlhttp.send();
    
    }, false);
    

    On page load, this will make a request to the server and refresh the table. You can add code where i've commented saying // Your request has gone through.

    NOTE: This code does not use any jQuery, and is written in completely original Javascript. I've never understood the logic in pulling down the whole jQuery toolkit, if you're only going to be making AJAX requests with it.

    Extra Reading