1

I am new in servlets, I would like to display results in the very same page that I am on when I click a search button, then the results should be on the very same page, how can I achieve that without going to another JSP or if I am supposed to do it behind the scene moving to another without the user noticing that its another page, how do I make it seem as if its the same page with results on it. Any shed of light is highly appreciated.

enter image description here

informatik01
  • 16,038
  • 10
  • 74
  • 104
Raymond Nakampe
  • 371
  • 7
  • 22
  • 1
    Do you want to send a synchronous (regular) or asynchronous (ajax) request? You didn't explicitly mention it, but you tagged JavaScript for some unclear reason. If the former, use the 1st Hello World example of [our Servlets wiki page](http://stackoverflow.com/tags/servlets/info). If the later, go through this answer to grasp the basic concepts based on some Hello World examples: http://stackoverflow.com/questions/4112686/how-to-use-servlets-and-ajax/4113258#4113258 – BalusC Apr 28 '13 at 19:34
  • Hi BalusC, I tagged JavaScript because I wanted to broaden my options of achieving what I wanted. I basically have a servlet which inside I setAttribute to my request as an arrayList of data. Out of interest, is a request dispatcher necessary in this where I want data to be displayed under the text boxes and the search button ? – Raymond Nakampe Apr 28 '13 at 20:42
  • 2
    Do you know better ways then? Emitting HTML in servlet? Oh please no. Just forward to a JSP and let it conditionally display the results in a `` block or so. – BalusC Apr 28 '13 at 23:04

3 Answers3

0

You have to use javascript to receive the search results from the server. This technique is called ajax. Javascript libraries like jquery (or many others) can help you a lot with this.

micha
  • 47,774
  • 16
  • 73
  • 80
  • Thank you Micha for your reply, I will look into that and shall tell you if I am encountering problems or if its a success. – Raymond Nakampe Apr 28 '13 at 16:51
  • This should be a comment instead of an answer. Note that BalusC's first comment covers this and even post the Q/A related with ajax and servlets. – Luiggi Mendoza Apr 29 '13 at 03:37
  • Even if it is short I would describe my post as a (very general) answer to a (very general) question. Therefore it is an answer for me and not a comment. – micha Apr 29 '13 at 19:00
0
  • Your form submits the search term to the current page, i.e. you can leave the form action attribute empty.
  • In your servlet check whether a search term has been submitted. If this is the case, execute your search function and write the result to the response.
Matthias Herlitzius
  • 3,365
  • 2
  • 20
  • 20
  • Thank you for your reply Matthias, I would like to display the results under the text boxes and the search button I have above so I can search as many times as I want without having to go back to search for the names again. – Raymond Nakampe Apr 28 '13 at 16:50
0

You don't mention where you data is coming from, but that is fairly unimportant in this question. The data could come from a local variable, from DOM storage or have been served to you using AJAX. But here is an example of setting text data in a textarea element (there are nearly unlimited ways to format what you want, this is one example only). Dynamically changing your current page in this manner means that you do not need to navigate to another.

It works by getting a reference to the element by it's name ("data" in this case) using document.getElementById

We then set the element's content value to your data, and it is displayed in the textarea.

HTML

<input type="text"></input>
<div>
    <textarea id="data"></textarea>
</div>

Javascript

var data = "Here is your data that was returned from your source";

document.getElementById("data").value = data;

On jsfiddle

Xotic750
  • 22,914
  • 8
  • 57
  • 79
  • Thank you for your insight Xotic I shall look into that out of interest, would it be possible to retrieve an object that has some data which is contained in my servlet which I can bring into my search page to display it there ? – Raymond Nakampe Apr 28 '13 at 16:59
  • Yes, that is exactly what the demo shows, in this case it was a string variable but it could easily have been an array or strings or a object of string from which you chose one of using your search mechanism. Here is someone else attempting to de exactly that: http://stackoverflow.com/questions/16247626/how-to-let-the-user-input-text-to-search-array-object-then-output-if-there-is-a/16247798#16247798 – Xotic750 Apr 28 '13 at 17:15