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.
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.
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.
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
Related
I have a problem that is not easy to summarize in title of the question. Here's the situation:
There is one very simple form that contains only one for selecting a xml and one submit button. When I submit this form, some work is done and I get org.w3c.dom.Document. This Document contains all information I need. Until now controller that was handling this, was returning page that renders this xml.
What I want to do now is except rendering xml, I want to display one or more pages that will contain some date that is inside this xml. Of course, depending of how many particular items there is in that xml, that much additional pages I will display, but I don't think it would be more than 3 or 4 in worst case scenario.
Any suggestion would be helpful, if this is even a possible.
I have a Spring MVC based JSP page which uses Displaytag to display a set of data. In the form on the page, the user can select either, or both of two checkboxes to display which subset of the data they want to see (Set "O", set "Q", or "B" both.)
The form and the display work fine on the first page, but the Displaytag pagination links at the bottom of the table contain malformed URLs. The result is that when the user clicks a link to display a different page, the checkboxes are reset and the user always sees "Both O and Q" even if they only wanted to see "Q" (for example).
I have added this parameter to the form that Displaytag uses to generate these URLs (according to the Displaytag documentation) but the value in the generated URLs does not change.
The form entry (type="o"):
<input type="hidden" id="type" name="type" value="o">
The pagination URL (type="b" for both):
http://localhost:8080/searchmain.html?d-443691-p=3&endDate=&_stateQuote=on&type=b&stateOrder=true
Can anyone shed any light on this?
Does anyone know how the URL parameters are generated?
Found the answer - posting it here so hopefully it will help someone else.
Displaytag generates the URL parameters for its pagination using the parameters on your initial HTTP REQUEST, not the RESPONSE. So if you are depending on some updated parameters coming back (as was this case), then your paging will produce incorrect results. In this case, the two checkboxes on the page were being used to update the hidden "type" field which was then passed back in the response. Unfortunately, the paging URLs were updated with the "type" field from the REQUEST which still contained the previous un-updated value (example "B") instead of the new updated value "O".
Whatever values you are hoping to find in the URL will have to be on the HTTP REQUEST you pass in, you cannot expect to utilize anything from the RESPONSE.
Makes sense, it just took a while to find it.
If you want to see the actual code, it's in this routine in TableTag.java in the Displaytag source:
protected void initHref(RequestHelper requestHelper)
I want to retrieve a set of results, which consist of all results produced by (looping) all the options of one of the request-form fields.
I'm using Java language, and HtmlUnit API.
I have managed to do this looping form-fill using the URL to 'fill' the field's variables (I don't know if its the best method, and actually am quite worried it's one of the worst...But it's the one i could do with the knowledge i have).
But i'm having problems figuring out how to make the program submit the form in order to reach the result page, and on how to download (scrape) that page before moving to the next.
NOTES:
-If you have a better way of filling the 'request-form', that is welcome as well.
UPDATE:
This solves the issues when using HtmlUnit API (thank you, touti):
HtmlPage resultado = pageNow.getElementByName("buscar").click();
System.out.println(resultado.asText());
A better way than loading both the request and response pages is still hugely welcome tough!
you can simulate using Jquery the click on your submit input like this
$("#submit_id").trigger("click");
I'm trying to build a simple GAE application that would read a textbox, pass its contents to a Java method, and write its results to a specific DIV.
I know the very basics of HTML and web concepts (CSS, DOM etc.), but I lack practical experience in this field.
How do I hook up the text field to the input of the method, and the DIV to its output?
Basically you need to create an ajax request to the server passing the contents of the textbox as a parameter and returning the results, and then when you get the results set the contents of the div to what was returned.
look at the jquery.get() method for the client side code. You'll need to define the success method as a function which will set the contents of the div with the returned data. something along these lines:
$.get(insert_url_to_call_here,{ textBoxData: text_box_value }, function(data){
$(insert_selector_of_div_here).html(data);
});
you don't say what technology you are using server side, but basically you need the url you hit to return the data you need, and this should append whatever is returned to the div, so returning a html fragment should be what you want to do.
I would like to let users customize pages, let's call them A and B. So basically I want to provide a hyperlink to a jps page with big text box where a user should be able to enter any text, html (to appear on page A), with ability to preview it and save.
I haven't really deal with this sort of issues before and would appreciate help on how implement it (examples and reference would be very helpful too)
Thanks
Are you using any kind of web framework(Spring MVC / Struts / Tapestry / etc...)? If you are, they all have tutorials on dealing with user inputs / form submission, so take a look at that. They all differ slightly in how user input is processed so it's impossible to answer this question generically.
If you're not (e.g. this is straight JSP), take a look at this tutorial.
Basically, what you want to do is to define an HTML form on your page B with textarea where user would input custom HTML. When form is submitted, you'll get the text user entered as a request parameter and you can store it somewhere (in the database / flat file / memory / what have you). On your page A you'll need to retrieve that text and bind it to request or page scope, you can then display it using <%= %> or <jsp:getProperty> tags.
To ChssPly76's answer I'd just add that if you're going to provide text entry of html on a web page (or anywhere, really) you're going to want to provide some kind of validation and a mechanism to provide feedback if the html is bad. You might dispense with this for a raw internal tool but anything for public consumption will need it. e.g. what do you do if someone enters
<b>sometext
You can deal with this with simple rules that parse away html tags, a preview that lets people know how they're doing so far ala stackoverflow, an rtf input option, or just a validate and if the tags don't balance a big honking "Try again", but you'll want some kind of check that you won't just be putting up broken pages.