How to call a servlet from a jQuery's $.ajax() function

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:

<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(){  


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

Email loading


And my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""    xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">


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 {

    PrintWriter out = response.getWriter();




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.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.