如何调用从jQuery的$。阿贾克斯()函数的一个servlet函数、jQuery、阿贾克斯、servlet

2023-09-10 13:23:08 作者:听说有人想取这个名儿

我想从jQuery的阿贾克斯调用一个servlet()函数。

目前,我不认为我甚至调用servlet或通过paramaters到它,但是似乎许多谷歌搜索并没有帮助。任何想法?

这是我的html:

 < HTML>
< HEAD>
< META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= ISO-8859-1>
<脚本类型=文/ JavaScript的SRC =jquery.js和>< / SCRIPT>
<脚本类型=文/ JavaScript的>
功能登录(){

  $(#加载)隐藏()。

  VAR电子邮件= document.nameForm.email.value;
  $阿贾克斯({
    键入:GET,
    网址:ProcessForm
    数据:电子邮件=+电子邮件,
    成功:函数(结果){
      警报(结果);
    }
  });
}
< / SCRIPT>
<标题>我AJAX< /标题>
< /头>
<身体GT;
< P>这一次,它是要去工作< / P>
<表格名称=nameFormID =nameForm方法=邮报行动=JavaScript的:登录()>
 

电子邮件                  加载

 < /身体GT;
< / HTML>
 
jquery里ajax中怎么将函数中的数据提取出来,放在另外一个其他函数中使用

和我的web.xml

 < XML版本=1.0编码=UTF-8&GT?;
< web应用程序的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns =htt​​p://java.sun.com/xml/ns/javaee的xmlns:网络= http://java.sun.com/xml/ns/javaee/web-app_2_5.xsdXSI:的schemaLocation =http://java.sun.com/xml/ns/javaee HTTP://java.sun。 COM / XML / NS / JavaEE的/ WEB-app_2_5.xsdID =WebApp_ID版本=2.5>
  <显示-名称>&ajaxtry LT; /显示-名称>
  <欢迎-文件列表>
<欢迎-文件>的index.html< /欢迎-文件>
<欢迎-文件>的index.htm< /欢迎-文件>
<欢迎-文件>的index.jsp< /欢迎-文件>
<欢迎-文件> default.html< /欢迎-文件>
<欢迎-文件>的default.htm< /欢迎-文件>
<欢迎-文件>的Default.jsp< /欢迎-文件>
  < /欢迎-文件列表>

  <的servlet>
< servlet的名称>&ProcessForm LT; / Servlet的名称>
<的servlet类> com.ajaxtry.web.ProcesFormServlet< / Servlet的类>
  < / servlet的>
   < Servlet映射>
< servlet的名称>&ProcessForm LT; / Servlet的名称>
< URL模式> / ProcessForm< / URL模式>
  < / Servlet映射>
< / web-app的>
 

该servlet只是此刻模板:

 包com.ajaxtry.web;

这里//进口

公共类ProcessFormServlet {

  公共无效的doPost(HttpServletRequest的请求,HttpServletResponse的响应)抛出IOException异常,ServletException异常{

    response.setContentType(text / html的);
    PrintWriter的输出= response.getWriter();

    的System.out.println(的request.getParameter(电子邮件));
  }
}
 

解决方案

几个问题在这里:

您所呼叫的System.out.println,这是刚刚发送输出到标准输出 - 而不是浏览器。尝试改变的System.out.println只是通过out.println

看起来你已经定义的doPost()在你的servlet code,但您的JavaScript使用的是GET的方法。重命名的doPost()来的doGet(),或定义他们两人。

话虽这么说,你可能不应该使用JavaScript打扰所有,直到你实际上已经得到了servlet的工作,以保持它的简单。您应该能够通过加载/ ProcessForm?电子邮件=测试在浏览器中测试一下,看看一些输出。一旦你得到的会,然后就可以开始担心前端code。

希望这有助于让你开始。

I am trying to call a servlet from jQuery's .ajax() function.

At the moment I don't think I am even calling the servlet or passing paramaters to it, however lots of Googling doesn't seem to have helped. Any ideas?

This is my html:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function login(){  

  $("#loading").hide();

  var email = document.nameForm.email.value;  
  $.ajax({  
    type: "GET",  
    url: "ProcessForm",  
    data: "email="+email,  
    success: function(result){  
      alert(result);
    }                
  });  
}        
</script>
<title>My AJAX</title>
</head>
<body>
<p>This time it's gonna work</p>
<form name="nameForm" id="nameForm" method="post" action="javascript:login()">

Email loading

</body>
</html>

And my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ajaxtry</display-name>
  <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
<servlet-name>ProcessForm</servlet-name>
<servlet-class>com.ajaxtry.web.ProcesFormServlet</servlet-class>
  </servlet>
   <servlet-mapping>
<servlet-name>ProcessForm</servlet-name>
<url-pattern>/ProcessForm</url-pattern>
  </servlet-mapping>
</web-app>

The servlet is just a template at the moment:

package com.ajaxtry.web;

// imports here

public class ProcessFormServlet {

  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    System.out.println(request.getParameter("email")); 
  }
}

解决方案

A couple of problems here:

You're calling System.out.println, which is just sending output to standard out - not to the browser. Try changing "System.out.println" to just "out.println"

It looks like you've defined doPost() in your servlet code, but your javascript is using the "GET" method. Rename doPost() to doGet(), or define both of them.

That being said, you probably shouldn't bother with the javascript at all until you've actually got the servlet working, to keep it simple. You should be able to test it by loading /ProcessForm?email=testing in your browser and see some output. Once you get that going, then you can start worrying about the front-end code.

Hope this helps get you started.