Skip to content
Related Articles

Related Articles

How to display search result of another page on same page using ajax in JSP?

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 30 Aug, 2019

In this example, we are creating a search bar to display the result on the same page in using ajax in JSP. AJAX stands for Asynchronous JavaScript and XML. Where Ajax is mainly used for Displaying another web page content on the same web page without refreshment of the page. You can read more about ajax technology here.

We use the JQuery ready function in order to make sure that our JavaScript does not execute until the page has loaded. Inside the ready function,
we perform a simple use click event with id="#sub" it is the id of the submit bottom on which the action should be performed.

var fn=$("#user_input").val();
$.post("SearchResult.jsp", {n3:fn}, function(data){    

In the above code, the

  • $(“user_input”).value(): Takes the input values from text field where user_input is the id.
  • $.post(“SearchResult.jsp”, {n3:fn}, function(data){}): In this case fn value is been pass as argument into index.jsp page which search result from database and display on the same page.fn value can be obtain in index.jsp by using request.getParameter("n3").
  • $(“#msg”).html(data): msg is the id of the div tag means, it will display the result only inside the div tag.


  • index.html

    <!DOCTYPE html>
        <title>search result of another page on 
          same page using ajax in JSP</title>
        <meta charset="UTF-8">
        <meta name="viewport"
                <h1 style="color:green">
                <h2>Enter Your Name</h2>
                <input placeholder="Search" 
                <button type="Submit" id="sub">
        <div class="box_1" id="msg">
            <script type="text/javascript" 
            <script src="jquery-3.2.1.js"></script>
                $(document).ready(function() {
                    $("#sub").click(function() {
                        var fn = $("#user_input").val();
                        $.post("index.jsp", {
                            n3: fn
                        }, function(data) {

  • index.jsp

             String uname=request.getParameter("n3");
             out.println("Welcome "+uname);



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!