jQuery AJAX JSP Servlet Java Example

Filed Under: Java EE

Ajax in Java JSP Servlet based web applications are very common. Recently I have written a lot about jQuery methods and how we can use them. Today we will look into one of the important jQuery functionality where we can easily execute AJAX calls and process the response in a Java Servlet JSP based web application.

Ajax JSP Servlet Example

I am using Eclipse IDE for creating the “Dynamic Web Project”, you can use any other IDE too. Our main focus will be towards jQuery and AJAX call from JSP to a servlet. Below image shows the final project structure.

AJAX Java, Ajax JSP, Ajax Servlet, Ajax JSP Example

Ajax Servlet Code

We have a very simple servlet that gets the userName from request, create a greetings and return it as plain text.

package com.journaldev.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String userName = request.getParameter("userName").trim();
		if(userName == null || "".equals(userName)){
			userName = "Guest";
		String greetings = "Hello " + userName;


Notice that I am using Servlet-3 annotations for configuration, if you like XML based configuration then you can do it in web.xml file. We will call this servlet asynchronously using jQuery AJAX support.

Ajax JSP Page

Below is our JSP page code, it has an input field where we can provide user name. As soon as focus is moved out of it, jQuery AJAX method will execute and call our servlet and process the response.

index.jsp code:

<%@ page language="java" contentType="text/html; charset=UTF-8"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery, Ajax and Servlet/JSP integration example</title>

<script src="https://code.jquery.com/jquery-1.10.2.js"
<script src="js/app-ajax.js" type="text/javascript"></script>

		Enter Your Name: <input type="text" id="userName" />

	<strong>Ajax Response</strong>:
	<div id="ajaxGetUserServletResponse"></div>

Notice that we have two JS files included in the JSP page, first one is the jQuery JS library and another one contains our JS code for ajax call. I am including jQuery JS from the code.jquery.com URL, we can also download it and keep with our JS file.

JSP code is very simple, we will populate ajaxGetUserServletResponse div content from the AJAX call response through jQuery.

jQuery AJAX JavaScript File

Below is our javascript file code for jQuery AJAX request.

app-ajax.js code:

$(document).ready(function() {
        $('#userName').blur(function(event) {
                var name = $('#userName').val();
                $.get('GetUserServlet', {
                        userName : name
                }, function(responseText) {

We can also make jQuery AJAX call using it’s ajax() method, as shown below. Above is the shorthand approach to using ajax() method.

$(document).ready(function() {
	$('#userName').blur(function() {
			url : 'GetUserServlet',
			data : {
				userName : $('#userName').val()
			success : function(responseText) {

Below is the syntax of the jQuery ajax() method, try to relate it to the above code and you will understand what’s going on here.

  url: url,
  data: data,
  success: success,
  dataType: dataType

Our jQuery Ajax JSP Servlet Example application is ready, just build and deploy it in your favorite servlet container. Below image shows the output produced, I am using Chrome Developer tools to confirm that our servlet is getting called.

jQuery AJAX Example, Ajax JSP, Ajax servlet, Ajax Java

Ajax JSP Servlet Example Summary

We learned the basics of jQuery AJAX support and how we can integrate it with Java web application, in next tutorials we will learn more features of jQuery that we can use in any web application. You can download the final project from the below link.


  1. hemant says:

    if i have multiple value then what should i do in java controller
    please tell me

  2. Eric Osman says:

    I got this example working easily. Thank you very much. Some specifics about my experience:
    1) I am using eclipse and tomcat.
    2) To make your servlet, I right-clicked in eclipse on my project in the project explorer and chose “new – > servlet” and then used your convenient copy button to help copy-paste your servlet code into my new servlet source file.
    3) I was nervous about using your code.jquery.com version of jquery since it is http and not https, but fortunately I didn’t need to use it, since my code already referred to cdnjs.cloudflare.com which is https. /Eric Osman 4-dec-2019
    Feel free to reach me here: eric osman at rcn dot com

  3. sivani sahoo says:

    Hi, i have a request kindly tell me can i call a .net service through this ajax call, if yes then how.

  4. Balakrishna says:

    Thanks it will helpful for me

  5. Moreno says:

    I’m trying to import your example.
    Usage Spring sts 3.9.6 Release.
    I am new to the environment and I would like to learn from your examples, but importing it as a maven project gives me a considerable number of errors.
    how can I do
    my mail is misonsan@libero.it
    I’m Italian and my name is Moreno

  6. Kunal bhide says:

    Hey buddy,
    Thank you so much for this code. You have saved my life.
    Last one week i was working on this ajax code but i didnt getting it.And finally i got it.

    Thanks again.

  7. gyana says:

    Not working for me . You may be missed to write regarding web.xml

    1. Girija says:

      for me also not woorking.

      1. Barry says:

        Not working for me, but I’m getting an error on the annotation.WebServlet class – giving me a compilation error with regards to this. I’ve seen all kinds of mixed information on adding it to the web-inf lib dir. Not sure if I can just go download the jar with it.

  8. shahjad says:

    Hello ,

    This Example is working fine..


  9. Sidharth says:

    Dear Pankaj,
    Thank for this program.
    I am not getting the desired output …when i enter the name “Guest” …I am not getting “Hello Guest” as an Ajax response. Build successfully(clean and install all).
    Kindly reply. thank you.

  10. Ath J says:

    There is not servlet class file in the example.There is source file but not class file.That’s why so many people were unable to run it.Actually he has correctly not compiled the file as the java versions of people would differ and they would not be able to run it.So first compile the servlet file to class file and then place it in the classes forlder and it would work

  11. huangjinku says:


    when request.getParameter(“userName”) is null ,there will throw an exception

  12. huangjinku says:

    not need web.xml?

  13. sanooj says:

    I want to know more….

  14. Radovan says:

    Thank you for a tutorial!
    As I understand, when user make any request on server, response from servlet is automatic send back to client? Without any call from sendRedirect() or forward() or include()?

    1. Pankaj says:

      You can do forward or sendRedirect when you want to respond with HTML response. Here we are sending plain text response from servlet because we want to show that in UI through Ajax calls. You can send the JSON or XML response as well and then parse it through JS at your client side code.

  15. Gouravmoy Mohanty says:

    Hi Pankaj,

    I find that this code is working perfectly in Chrome. I wrote a similar in which there is a ajax call function that connects to the server every 5 sec and fetches the date and time. it worked perfectly in chrome but not working for IE. Its only performing one call for IE and then stopping. Can you suggest a fix?

  16. yuanyu says:

    Thanks! This example help me out how the ajax could be implement by javascript and jsp!

  17. Emran Hossain says:

    Sir , thank you very much .It works .

  18. Eshan says:

    How to return the same data in json format?

  19. Antony Sebastin C says:

    Wanna become a versatile developer in JAVA domain, could you help me..?

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content