javascript 如何使用ajax从javascript调用servlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27127263/
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-10-28 06:59:41  来源:igfitidea点击:

how to call servlet from javascript using ajax

javascriptajaxjspservlets

提问by Vikas Reddy

I am doing a samall jsp page to search a name enter in text box .. i called javascript function from jsp .. the bello is javascript code

我正在做一个小jsp页面来搜索在文本框中输入的名称..我从jsp调用了javascript函数..bello是javascript代码

function fncStudsearch()
{
//alert("yes")
var ele=document.getElementById("stdSearch").value;
var xmlhttp;
var strAjUrlData="stdSearch?key="+ele;
//alert(strAjUrlData)
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 && xmlhttp.status==200)
{

}
  else
  {
  alet(xmlhttp.status);
  }
 }
  xmlhttp.open("GET",strAjUrlData,true);
      xmlhttp.send();

  }

I am calling servlet .. and i configured web.xml as follows

我正在调用 servlet .. 我配置了 web.xml 如下

   <servlet>  
      <servlet-name>stdSearch</servlet-name>
       <servlet-class>com.slokam.Act.StudentSearch</servlet-class>
  </servlet>

<servlet-mapping>
     <servlet-name>stdSearch</servlet-name>
     <url-pattern>/stdSearch</url-pattern>
</servlet-mapping>

</web-app>


I am unable ti go to servlet class and servlet code i have written is


我无法转到 servlet 类,我编写的 servlet 代码是

   public class StudentSearch extends HttpServlet {


   private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    String stdkey=request.getParameter("key");
    stdkey="%"+stdkey+"%" ;
    System.out.println(stdkey);
  }
}

please help in this regard how to goto servlet

请在这方面帮助如何转到 servlet

回答by Si Kelly

If the app is not deployed as the root application on the appserver, you might need the context path in the url you are calling:

如果应用程序未部署为应用程序服务器上的根应用程序,则您可能需要调用的 url 中的上下文路径:

var ctx = "${pageContext.request.contextPath}/";
var strAjUrlData=ctx+"stdSearch?key="+ele;

...

This code assumes you are using jsp 2.0 and EL

此代码假设您使用的是 jsp 2.0 和 EL

回答by Si Kelly

Possibly this might help, there is an javascript error:

可能这可能会有所帮助,有一个 javascript 错误:

alet(xmlhttp.status); // you're missing here `r`

alert(xmlhttp.status);

Secondly, you have to print out some contents from servlet, use PrintWriter for that.

其次,您必须从 servlet 打印出一些内容,为此使用 PrintWriter。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String stdkey=request.getParameter("key");
    stdkey="%"+stdkey+"%" ;
    // test purpose
    PrintWriter pw = response.getWriter ();
    pw.print(stdkey);
    pw.close();
}