0

I want to call a servlet in JavaScript, but how to call I do not know.

function func_search()
{
    var srchdata = document.getElementById('searchitem').value; 

    if(srchdata == "")
    {
        alert("Enter Search Criteria...");
    } 
    else 
    {
        //what to write here to call servlet ??
    }               
}

<a onclick="func_search();"><img src="images/srch.png" height="32px" width="32px"/></a>
1
  • The terminology is 'navigation', not 'calling'. You call a method, you navigate to a web resource (or rather: you make the browser do it). I say so because using proper terminology will provide you with more relevant Google search results in the future. Commented Dec 18, 2013 at 13:19

3 Answers 3

4

document.location.href is used

function func_search()
            {
                var srchdata = document.getElementById('searchitem').value; 
                //alert(srchdata);  
                if(srchdata == "")
                {
                    alert("Enter Search Criteria...");
                }
                else
                {
                    document.location.href="your servlet name here";    
                }               
            }
Sign up to request clarification or add additional context in comments.

Comments

1

Servlets are mapped to URL pattern, so just need to make a call to that url (post/get/ ...) Create a an ajax request object and make a call. explore on JavaScript ajax methodologies.

http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp

Comments

0
     function callServlet()
 {
     document.getElementById("adminForm").action="./Administrator";
     document.getElementById("adminForm").method = "GET";
     document.getElementById("adminForm").submit();
 }

<button type="submit"  onclick="callServlet()" align="center">Register</button>

This way you can do it !!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.