admin管理员组

文章数量:1297025

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>

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>
Share Improve this question edited Dec 18, 2013 at 13:22 Andrei Nicusan 4,6231 gold badge27 silver badges37 bronze badges asked Dec 18, 2013 at 13:14 user2293418user2293418 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. – Gimby Commented Dec 18, 2013 at 13:19
Add a ment  | 

3 Answers 3

Reset to default 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";    
                }               
            }

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./ajax/ajax_xmlhttprequest_send.asp

     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 !!!

本文标签: javahow to call servlet in JavaScriptStack Overflow