Accessing HTML DOM elements from Java - java

I'm developing (with Java) a P2P application. One of the features includes a chat service. When a user sends a message to all of the application users, each user gets the message and updates its chat HTML page.
How can I access, from my Java code, the DOM of this page and change it, without the need to refresh the page in order to see the new message?
Is there any object in Java that can get me this access? For example, can I call a JavaScript function that inserts the new message?

If by from Java you mean applet then:
You can define some javascript functions in your HTML page to return/modify what you want and then call the javascripts from the applet. Look at here.
If by Java you mean Web server then you have to use some AJAX solution, you can look for example at JQuery

What you're really looking for is a technology known as Comet. Comet is Reverse Ajax. It's a technique that uses long-lived HTTP connections to hold a connection open from a client browser to a server so that the server can push updates back to the client browser.
The basic flow is that the server pushes a command back to the browser in the response, and JavaScript parses the response via a callback function, and then the JavaScript updates the DOM, all without reloading the page.
You can learn more about Comet on the CometD Website, and if you're developing on Google App Engine, this blog post on the ChannelAPI will be helpful.

Related

How to refresh jsp page with ajax after a DB change

I have many clients that connect to a webservice.
Everytime a client perform an action on the DB I want that all client can see the modify, by an updating of the jsp page that show the content of the DB.
How can I do this in ajax?
You can simply use websocket to refresh and renew page.
use this link what to use websocket:
Jsp+tomcat7.047+html5 simple demo based on websocket
It is impossible by simply using ajax because the client (browser) don't know wwhen data is updated, and in client / server model, server can't send request to client to tell him.
Instead you should take a look at websockets. For example here :
http://www.java2s.com/Tutorial/Java/0360__JSP/CreatingClientServerApplications.htm
Otherwise, you could simply refresh your web page by sending ajax requests periodically. If you are new to javascript, I recommand using jquery. The way to do it depends on your needs.
If you prefer to reload the whole page, see this answer https://stackoverflow.com/a/5404869/5970908.
To reload a portion of the page, you need to query a template, to get the refreshed html. Then insert it somewhere in the DOM. In this case, refer to the following topic
https://stackoverflow.com/a/9436594/5970908

How to append html to a website response before it reaches the browser in Java?

Recently I used a Mac application called Spotflux. I think it's written in Java (because if you hover over its icon it literally says "java"...).
It's just a VPN app. However, to support itself, it can show you ads... while browsing. You can be browsing on chrome, and the page will load with a banner at the bottom.
Since it is a VPN app, it obviously can control what goes in and out of your machine, so I guess that it simply appends some html to any website response before passing it to your browser.
I'm not interested in making a VPN or anything like that. The real question is: how, using Java, can you intercept the html response from a website and append more html to it before it reaches your browser? Suppose that I want to make an app that literally puts a picture at the bottom of every site you visit.
This is, of course, a hypothetical answer - I don't really know how Spotflux works.
However, I'm guessing that as part of its VPN, it installs a proxy server. Proxy servers intercept HTTP requests and responses, for a variety of reasons - most corporate networks use proxy servers for caching, monitoring internet usage, and blocking access to NSFW content.
As a proxy server can see all HTTP traffic between your browser and the internet, it can modify that HTTP; often, a proxy server will inject an HTTP header, for instance; injecting an additional HTML tag for an image would be relatively easy.
Here's a sample implementation of a proxy server in Java.
There are many ways to do this. Probably the easiest would be to proxy HTTP requests through a web proxy like RabbIT (written in java). Then just extend the proxy to mess with the response being sent back to the browser.
In the case of Rabbit, this can either be done with custom code, or with a special Filter. See their FAQ.
WARNING: this is not as simple as you think. Adding an image to the bottom of every screen will be hard to do, depending on what kind of HTML is returned by the server. Depending on what CSS, javascript, etc that the remote site uses, you can't just put the same HTML markup in and expect it to act the same everywhere.

how to make a servlet refresh a jsp page if there is new information to be displayed?

I am making a web application that receives pictures from an android smartphone and then displayes them in a .jsp page.
I need a mechanism to reload the .jsp page if there is new pictures in the server. Kinda like facebook where we dont need to refresh to see new content.
Does facebook just refreshs from time to time or there is some mechanism that realizes there is new information to be displayed and notifies the page to refresh itself?
As the other folks answered, with HTTP the client (web browser) must initiate the message exchange. In other words. the server (JSP is a server process) cannot send a message to a web browser. The web browser must start the process (by sending a request). The server can only respond the web browser's request.
So as the other folks pointed out. Face book probably sends a javascript program to the web browser. The javascript program running in the web browser every few seconds sends a message to server. The message asks the server whether there is new information to display.
Hope that makes sense. You need to add javascript that runs in the browser. That javascript runs in a loop, polling the server for new info. The server does not poll the web browser. It's the other way around. The web browser polls the server.
Hope that helps :) Good Luck.
The servlet only informs that there is new information then, the jsp refreshes itself. The other option is to use AJAX so that the servlet returns you the new information let's say in a JSON format. And then by using javascript you update your webpage with the new information (and without refreshing the page because of AJAX).

Scraping with Javascript within Client Browsers

Scraping websites with PHP or Java is easily implemented, my question is however if I would want to have the client computer scrape, if I could do this with javascript rather than on the server side.
The background is that websites could block my servers or server farms, however if I let the user computer scrape and then post that information to my server we would avoid that blockage of the servers.
Can we scrape a website with javascript and use CSS selectors or regular expressions to parse the HTML in order to extract certain information?
Will we be able to protect our code that we use in the javacript or will our scraping algorithm have to be human readable to people?
If we then post the results via AJAX to our server, how do we make sure it was our script and not manipulated data by a malevolent user?
Is there a good framework for accomplishing this or should I continue server-side scraping?

Java - reading data from website requiring login

I'm basically trying to use Java to read some data from my school's website (homework assignments, which lessons I have and when, etc.) for personal use. However, my school requires one to be logged in to access this information.
Could anyone point me in the right direction for logging in with code and accessing this information?
Thanks,
Mike.
The Apache has a API for http client simulating.
Link: http://hc.apache.org/httpcomponents-client-ga/
You need to find out how the server handles logins. What authenticationer it used: cookies, url session id, etc.
Then you can send the server a login http form submit (as you would have done manually) and save the authenticationer.
With the authenticationer you can then access the secured sites.
I would use HtmlUnit, which is a programmatic web browser.
You tell it to load the authentication page, to fill the form with your credentials and to click on the submit button. The you click on links, just as you would do it with a real browser, but programmatically, using Java instructions.
And it even supports JavaScript, if necessary.

Categories