I have a webpage that has JavaScript in it. The script contains a method that updates the webpage. I also have a java UDP server. When I get some parameters from a client, I want to call the method in the javascript to update the page.
Is it possible to call methods in Javascript from Java source code? Any pointers?
Thanks!
EDIT: For Ajax, the "request" initiates from the webpage. I want something that can change the webpage by itself - without this request.
A more succinct question would be: Can I dynamically update a webpage from java source file?
In order to reading javascript result you need a browser runtime AFAIK (You cannot get javascript result through a raw socket). You have to include a browser (JTextPane should be able to do it) into your udp server.
DWR is the answer, but it seems dead with no progress for some months. I don't think so you can directly call JavaScript methods from Java without passing an Asynchronous call using Ajax.
I have no idea on how you would dynamically change your content of webpage without an request being passed.
This is what we wanted to do:
-Send co-ordinates from an android phone to a server
-Plot these on a map in a browser window
The complexity was - One 'box' was server for android, and client for google maps. And we needed some glue between these two functionalities.
We initially tried ActiveMQ but could not get it to work.
Due to time constraints, we were forced to explore other approaches... our end result isn't elegant, but it works.
We have a FIFO on the server to which the co-ordinates are written. On the same server, our map page is also hosted. On a button press, XMLHttpRequest is sent to the server. In response, a co-ordinate is dequeued and sent back, which is plotted on the map using google maps api.
I will be happy to share more details/answer questions...
Related
I'm building my own HTTP server in java, but i'm facing with a problem: I would like to build a page dynamically by creating every HTML object at runtime, the question is: how can i determine the screen dimension of the client's browser?
This information is not present in the HTTP header, so I was thinking about writing a "fake" webpage that runs a javascript that tells the server about the screen (it should redirect to something like www.website.com/w:1920,h:1080) but I don't know anything about cookies (that I suppose are essential to store those informations).
Do you think that I should learn somthng about cookies or there's another way?
BTW I'm not using servlets, just Socket, because that's what I know... should I use servlets?
Thanks for your time!
Matteo
Server knows nothing about client's screen until client send this information. Javascript is easiest way to determine screen size:
window.screen.availHeight
window.screen.availWidth
AJAX request can be used to send the information to the server where it can be stored in session data and backed in database for example if the user is logged in or identified somehow. In such case you don't need cookies. However solution with cookies is easier, check how to set them via javascript. But I'm afraid such solution would be a bit of non-standard, if your site is gonna depend on javascript why not to use it extensively and generate all objects on client side, get that lazy computer working and save your server's resources :) Just feed data by sending simplest HTML containing script doing the work.
Servlets? Can be really light-weight and done with minimal knowledge if you have time go for it.
There's a butt on a website that uses the onClick javascript function to run a JS function called HiddenUsers.show() or HiddenUsers.hide()
In my Java code, I've established an connection to this site using the HttpURLConnection class. I'm able to post login credentials with a BufferedOutputStream and I can get responses with an InputStream. But is it possible to send the command for that makes that button respond as clicked in turn triggering the JS function on the server side, or even better call the JS function itself from within my Java code?
is it possible to send the command for that makes that button respond as clicked in turn triggering the JS function on the server side
Perhaps I'm not fully following, but Javascript runs on the client (eg in the Browser), not on the server. So it doesn't make much sense to send javascript code to a webpage through HttpURLConnection. You can however, look through the webpage source and scripts and figure out what happens when the button is pressed and respond. For instance, if the button is a submit button you can use a HttpURLConnection to send get/post data from the form data associated with that button.
or even better call the JS function itself from within my Java code
You can call javascript code through a javascript ScriptEngine contained with java, but javascript calls that change the DOM of a webpage would be out of context.
There is a junvenile joke in your first sentence.
Outside of that, no, this isn't really possible. I assume you're trying to automate gets and posts to another server from java. It will receive the javascript, HTML etc... but it will not execute the javascript unless you go out of your way to get a javascript engine to execute it, and then what good will it do you to "show" something?
I'm assuming your angle is to acquire the users that are "hidden". The users may already be in the HTML markup somewhere, in which case you'll just want to parse the HTML you get from your response and get them. It is also possible that the ".show()" function gets the user list via an ajax call. If that is the case your best bet will be to figure out what that ajax call is, and process it directly with another HttpURLConnection.
I want to activate the back button of the HTML-browser. with a JAVA command not Javascript.
does anyone know how?
Assuming you are talking about an applet: I assume you would have to use the netscape.javascript packages to hit the JS API for it.
If you are talking about server side Java, then you have no way to trigger the back functionality. The closest you could come would be to read the referer (warning: Optional! Sometimes forged! Make sure it is a URI on your domain!) and issue a Location header to redirect forwards to it.
You can't. Java runs on the server. JavaScript runs on the client. You could have a JSP output JavaScript which is then run at client time in the browser.
I need to scrape French court cases for a project, but I can't figure out how to get Java to navigate the Court's search engine.
Here's the search page I need to manipulate. I want to start scraping the results page, but I can't get to that page from Java with just the URL. I need some way to have Java order the server to execute a search based on my date parameters (01/01/2003 - 30/06/2003), and then I can run the show by simply manipulating the URL I'm connecting to.
Any Suggestions?
First make sure the terms of service for the site allow this.
I would httpclient posts to send the data and get the results. See the form on the page, figure out which variables you need to emulate and submit them with httpclient. You should get back the results you are looking for. Also this page has lots of javascript, so you need to figure out what it is doing, maybe its never submitting the form but making ajax calls to update the page, but maybe you can get the same results.
You can always install something like "fiddler" and watch the http traffic the page is sending and then emulate that using httpclient.
I would like to open a webpage and run a javascript code from within a java app.
For example I would like to open the page www.mytestpage.com and run the following javascript code:
document.getElementById("txtEmail").value="test#hotmail.com";
submit();
void(0);
This works in a browser...how can I do it programatically within a java app?
Thanks!
You can use Rhino to execute JavaScript but you won't have a DOM available - i.e. document.getElementById() would work.
You can use HTML Unit (headless) or WebDriver/Selenium (Driving a browser) to execute JavaScript in an environment that has a DOM available.
I'm not sure what you are looking for but I assume that you want to write automated POST request. This can be done in with Http Client library. Only you have to set appropriate request (POST or GET) parameters.
Look at examples - with this library you can do basic authentication or post files too.
Your question is a bit ambiguous, as we don't know the position of the Java program.
If that's a Java applet inside your page, you should look at Java<->JavaScript interaction, it works well.
If you need a separate Java program to control a browser, like sending a bookmarklet in the address bar (as one of your tags suggests), it is a bit harder (depends on target browser), perhaps look at the Robot class.
There's Rhino JS engine written in Java that you can run on app server such as Tomcat and feed JS to, however - it's not clear what are you trying to do with this?
There's also Envjs simulated browser environment which is based on Rhino but complete enough to run jQuery and/or Prototype
DWR (and other frameworks) now support "reverse ajax." The general idea is that you use one of three methods to communicate back to the client:
Comet (long-lived https session)
Polling
opportunistic / piggy-back (i.e. next time a request comes from the client, append your js call)
Regardless of method (which is typically a configuration-time decision and not a coding issue), you will have full access to any/all js calls you want to make.
Check out the reference page from DWR to get a pretty good explanation.