How to display search result of another page on same page using ajax in JSP?
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.
Approach:
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){ $("#msg").html(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.
Example:
-
index.html
<!DOCTYPE html><html><head><title>search result of another page onsame page using ajax in JSP</title><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"></head><body><center><div><h1style="color:green">GeeksforGeeks</h1><h2>Enter Your Name</h2><inputplaceholder="Search"type="text"id="user_input"/><buttontype="Submit"id="sub">Search</button></div></center><divclass="box_1"id="msg"><scripttype="text/javascript"src=</script><scriptsrc="jquery-3.2.1.js"></script><script>$(document).ready(function() {$("#sub").click(function() {var fn = $("#user_input").val();$.post("index.jsp", {n3: fn}, function(data) {$("#msg").html(data);});});});</script></div></body></html>chevron_rightfilter_none - index.jsp
<html><body><center><%String uname=request.getParameter("n3");out.println("Welcome "+uname);%></center></body></html>chevron_rightfilter_none
Output
Before:

After:

Recommended Posts:
- How to redirect a page to another page in HTML ?
- How to show Page Loading div until the page has finished loading?
- CSS | @page rule
- ES6 | Page Printing
- Refresh a page using PHP
- ES6 | Page Redirect
- Page Object Model (POM)
- How to get the title of an HTML page ?
- Download web page using Java
- CSS page-break-before Property
- CSS | page-break-after Property
- How to refresh a page using jQuery?
- How to add innerHTML to print page?
- PHP Program to count Page Views
- How to hide an element when printing a web page using CSS?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



