最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to call a servlet from a jQuery's $.ajax() function - Stack Overflow

programmeradmin3浏览0评论

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=""    xmlns="" xmlns:web=".xsd" xsi:schemaLocation=" .xsd" id="WebApp_ID" version="2.5">
  <display-name>ajaxtry</display-name>
  <wele-file-list>
<wele-file>index.html</wele-file>
<wele-file>index.htm</wele-file>
<wele-file>index.jsp</wele-file>
<wele-file>default.html</wele-file>
<wele-file>default.htm</wele-file>
<wele-file>default.jsp</wele-file>
  </wele-file-list>

  <servlet>
<servlet-name>ProcessForm</servlet-name>
<servlet-class>.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 .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")); 
  }
}

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/2001/XMLSchema-instance"    xmlns="http://java.sun./xml/ns/javaee" xmlns:web="http://java.sun./xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun./xml/ns/javaee http://java.sun./xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ajaxtry</display-name>
  <wele-file-list>
<wele-file>index.html</wele-file>
<wele-file>index.htm</wele-file>
<wele-file>index.jsp</wele-file>
<wele-file>default.html</wele-file>
<wele-file>default.htm</wele-file>
<wele-file>default.jsp</wele-file>
  </wele-file-list>

  <servlet>
<servlet-name>ProcessForm</servlet-name>
<servlet-class>.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 .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")); 
  }
}
Share Improve this question edited Jan 14, 2013 at 16:05 Will 14.5k1 gold badge44 silver badges44 bronze badges asked Dec 2, 2009 at 3:46 AnkurAnkur 51.1k114 gold badges246 silver badges316 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

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.

发布评论

评论列表(0)

  1. 暂无评论