array data populating issues with Javascript - java

I have a array of data which was generated due to some Actions in my previous actions
On submitting that page, Control will be redirecting to the second page,
IN the second page, I need to populate the array data into the Tables
( I want to use Javascript to populate this, No other means)
Problems which i am facing now
1) I cant read the Java array in the Java script ? (I am not sure how to pass the Java array to Javascript function)
2) Thought of implementing the Java script objects similar to the java objects, but there will be lot of over head
Can any one help me to over come this situation
I am using the BTT framework which is similar to Struts, for Javascript I am not using any frameworks
Thanks in advance

JSON would be a format to make the java-array accessible in javascript.
https://github.com/douglascrockford/JSON-java/blob/master/JSONStringer.java

too broad to answer, since specifics are missing. here are some generic steps.
Collect your stuff in JavaScript array
Have a hidden form field
while posting to server, set the value of hidden field to your array (in string format)
Read the hidden field content in server (posted form data)
Parse them (JSON is appropriate here)
Use them while preparing next page content as JS Array

Related

Java - use searchbar on given website

Let me just start by saying that this is a soft question.
I am rather new to application development, and thus why I'm asking a question without presenting you with any actual code. I know the basics of Java coding, and I was wondering if anyone could enlighten me on the following topic:
Say I have an external website, Craigslist, or some other site that allows me to search through products/services/results manually by typing a query into a searchbox somewhere on the page. The trouble is, that there is no API for this site for me to use.
However I do know that http://sfbay.craigslist.org/search/sss?query=QUERYHERE&sort=rel points me to a list of results, where QUERYHERE is replaced by what I'm looking for.
What I'm wondering here is: is it possible to store these results in an Array (or List or some form of Collection) in Java?
Is there perhaps some library or external tool that can allow me to specify a query to search for, have it paste it in to a search-link, perform the search, and fill an Array with the results?
Or is what I am describing impossible without an API?
This depends, if the query website accepts returning the result as XML or JSON (usually with a .xml or .json at the end of url) you can parse it easily with DOM for XML on Java or download and use the JSONLibrary to parse a JSON.
Otherwise you will receive a HTML that is the page that a user would see in a browser, then you can try parse it as a XML but you will have a lot of work to map all fields in the HTML to get the list as you want.

Request.getParameter java to vb.net

I have a question about retrieving data from a client who is using java and i am using vb.net.
I am expecting a form posted to me and read the data.
My issues is when i do Request.Form("DATA") i get nothing from the client.
Now if i create a html form and post it to my url with the field "DATA" i can read everything fine. I can also loop through my form and see the field and the button if i right them out to the screen or to a text file. Code is below
response.write(Request.Form("DATA"))
OR
Dim entryName As String
For Each entryName In Request.Form
response.write("Entity Name: " & entryName)
Next
Either method above works fine for me but not for the client. When the client hits my page i see nothing at all no buttons no fields, nothing.
I am concerned he is not posting properly to me. I spoke with the developer and he said he would retrieve the data on his end by doing something like "Request.getparameter"
I do not know java at all but from what i read it sounds like "Request.getparameter" will grab any field out of a form or querysting that has the name specified aka my "DATA" field that i am looking for.
Can anyone explain to me what request.getparameter means in java and what the equivalent code would be in vb.net?
Again i do not know java at all and have searched on this for a while but cant quite find a definitive answer.
Thanks in advance.
It is correct that in Java, request.getParameter("DATA") will look in both the query string and posted form data, while in .NET, Request.Form("DATA") only looks at posted form data. Therefore, it seems likely that your client is sending the data in the query string, since you are not seeing it.
You have a few options. You could use Request.QueryString("DATA") to check only the query string, or either Request.Item("DATA") / Request("DATA") or Request.Params("DATA") to check both the query string and posted form data, plus cookies and server variables. I think Items and Params may be a little different in what they return, e.g. for multiple values. They are probably the closest equivalent to the Java request.getParameter function.

How to get Javascript var value in Java (wicket 6)

I have a fairly simple question, but I'm unable to find a working answer.
I'm using javascript in my HTML file for a page that is taking input from the page and giving me back a string of a jSON object. All that I want to do is take this string and put it in a Java string.
I can do things like
target.appendJavaScript("s=getString();");
target.appendJavaScript("alert(s);")
which give me the desired information in the alert. But how do I get s into a Java string?
I have tried the following
StringValue temp = RequestCycle.get().getRequest().getQueryParameters().getParameterValue("s");
info(temp.toString());
And other variations like getRequestParameters(), but I get nulls on temp.
These actions are inside an AjaxFallbackButton's onSubmit.
Any advice on how to get a javascript var in Java?
Although I would still like to know more about sharing variables between Java and Javascript in HTML, my case was solved thanks to Wicket.
The value that I was retrieving with s=getString() was already accessible in a HiddenField, and so I simply needed to get it a wicket:id in the HTML and add it with a property model in the Java. Hopefully this might help someone else in the future.

How do I send a query to a website and parse the results?

I want to do some development in Java. I'd like to be able to access a website, say for example
www.chipotle.com
On the top right, they have a place where you can enter in your zip code and it will give you all of the nearest locations. The program will just have an empty box for user input for their zip code, and it will query the actual chipotle server to retrieve the nearest locations. How do I do that, and also how is the data I receive stored?
This will probably be a followup question as to what methods I should use to parse the data.
Thanks!
First you need to know the parameters needed to execute the query and the URL which these parameters should be submitted to (the action attribute of the form). With that, your application will have to do an HTTP request to the URL, with your own parameters (possibly only the zip code). Finally parse the answer.
This can be done with standard Java API classes, but it won't be very robust. A better solution would be HttpClient. Here are some examples.
This will probably be a followup question as to what methods I should use to parse the data.
It very much depends on what the website actually returns.
If it returns static HTML, use an regular (strict) or permissive HTML parser should be used.
If it returns dynamic HTML (i.e. HTML with embedded Javascript) you may need to use something that evaluates the Javascript as part of the content extraction process.
There may also be a web API designed for programs (like yours) to use. Such an API would typically return the results as XML or JSON so that you don't have to scrape the results out of an HTML document.
Before you go any further you should check the Terms of Service for the site. Do they say anything about what you are proposing to do?
A lot of sites DO NOT WANT people to scrape their content or provide wrappers for their services. For instance, if they get income from ads shown on their site, what you are proposing to do could result in a diversion of visitors to their site and a resulting loss of potential or actual income.
If you don't respect a website's ToS, you could be on the receiving end of lawyers letters ... or worse. In addition, they could already be using technical means to make life difficult for people to scrape their service.

Why does GWT wrap() method iterate the whole DOM unnecessarily?

I'm using a custom GWT component that wraps around an existing textbox in my HTML page.
The page returns a list of information - so as larger sets of information are loaded - the GWT loading process takes longer and longer.
Looking at the source code of the wrap() method - it appears it iterates through the DOM looking for matching ids.
Isn't this unnecessary? Is there a way to make it just iterate to my component and then stop?
GWT does a bunch of DOM house keeping and you are just not going to get around it easily or at all.
It sounds like there's a point at which you have so many text inputs that you need to rethink how you're approaching this anyway. Dynamically creating inputs fields in a form panel in GWT is pretty easy and fast, and you can very simply and quickly download a json structure with the data you need for the input fields in your original html page load, convert it to a dictionary or simple array in GWT and , use it to populate your form.
Once done, you can clear the pointer to the data so it will GC'd if you don't need it any longer.
To access data in javascript look at creating a native method, its very easy to do. If it makes sense, you can format the json data as a dictionary and GWT's dictionary class will map directly to it.
I use these techniques all the time and they are robust and pretty much as fast as javascript can populate the DOM.

Categories