Calling a JavasScript function on a remote server from inside my JavaCode - java

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.

Related

How to capture JS varible value sin a different java program

I have a JS program handling the communication from iframe to its parent. I am logging the different states on browser window (Inspect element's) console. How could i fetch these state values into my java program for validating/testing. The values are passed from JS in html file. Or if anyone can suggest any other way to validate the communication between parent and iframe.
Any suggestion or guidance will be helpful to move forward, I am stuck.
Thanks alot!!
Browser make http requests - so try implementing a basic http server in your java program and have the browser make ajax calls to the program.

java httprequest fetch the content of page to wait until javascript is done executing

I have a piece of Java code for getting the content of a webpage. The partial content of the webpage will be loaded/retrieved by a javascript once the page is loaded. That is, the page contains a javascript which will load the rest of the content. In Java, I want to wait until that partial content is loaded before start the rest of process. Is there anyway to do that?
Java is typically a server-side technology while JavaScript is a client-side technology. So if you want Java to wait for something on the client, it can't, because typically Java's job is to send something static (e.g. HTML) to the client. That being said, Java can render JavaScript code that will be run by the browser, and that JavaScript code can continue the process.
You can use AJAX to request the rest of the content from the server once the javascript processing is done.

Back button HTML-page access with JAVA

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.

Calling JavaScript methods from Java

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...

How to use Java to navigate a Web Search

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.

Categories

Resources