如何在 JavaScript 中调用 servlet?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20659231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:23:17  来源:igfitidea点击:

how to call servlet in JavaScript?

javajavascriptservlets

提问by

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

我想在 JavaScript 中调用一个 servlet,但是我不知道如何调用。

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>

采纳答案by Java Curious ?

document.location.hrefis used

document.location.href用来

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";    
                }               
            }

回答by harshal

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.

Servlet 映射到 URL 模式,因此只需要调用该 url (post/get/ ...) 创建一个 ajax 请求对象并进行调用。探索 JavaScript ajax 方法。

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

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

回答by Sanket Parchande

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

这样你就可以了!!!