I try to readout the Office 365 Website to compare it to a proxy configuration. But i cant get the select right so that it just gets me a specific section of those urls and ip addresses.
public class Office365WebsiteParser {
Document doc = null;
String WebseitenInhalt;
public void Parser() {
System.setProperty("http.proxyHost", "xxx");
System.setProperty("http.proxyPort", "8081");
System.setProperty("https.proxyHost", "xxx");
System.setProperty("https.proxyPort", "8081");
for (int i = 1; i <= 5; i++) {
try {
doc = Jsoup.connect("https://technet.microsoft.com/de-de/library/hh373144.aspx").userAgent("Mozilla").get();
break; // Break immediately if successful
} catch (IOException e) {
// Swallow exception and try again
System.out.println("jsoup Timeout occurred " + i + " time(s)");
}
}
if (doc == null) {
System.out.println("Connection timeout after 5 tries");
} else { // Wenn alles funktioniert hat Webseite auswerten
Elements urls_Office365_URLs = doc.select("div.codeSnippetContainerCode");
// HTML auswahl der Webseite nach div class und div id
// urls_Office365_URLs_global = urls_Office365_URLs;
WebseitenInhalt=urls_Office365_URLs.text();
}
}
public void Print() {
System.out.println(WebseitenInhalt);
}
public String get() {
return WebseitenInhalt;
}
}
I just want to select the containers like this:
<div class="codeSnippetContainerCodeContainer">
<div class="codeSnippetToolBar">
<div class="codeSnippetToolBarText">
<a name="CodeSnippetCopyLink" style="display: none;" title="In Zwischenablage kopieren" href="javascript:if (window.epx.codeSnippet)window.epx.codeSnippet.copyCode('CodeSnippetContainerCode_0f6f9acf-6aa4-471f-8600-f8d059f95493');">Kopieren</a>
</div>
</div>
<div id="CodeSnippetContainerCode_0f6f9acf-6aa4-471f-8600-f8d059f95493" class="codeSnippetContainerCode" dir="ltr">
<div style="color:Black;"><pre>
*.live.com
*.officeapps.live.com
*.microsoft.com
*.glbdns.microsoft.com
*.microsoftonline.com
*.office365.com
*.office.com
Portal.Office.com
*.onmicrosoft.com
*.microsoftonline-p.com^
*.microsoftonline-p.net^
*.microsoftonlineimages.com^
*.microsoftonlinesupport.net^
*.msecnd.net^
*.msocdn.com^
*.msn.com^
*.msn.co.jp^
*.msn.co.uk^
*.office.net^
*.aadrm.com^^
*.cloudapp.net^^
*.activedirectory.windowsazure.com^^^
*.phonefactor.net^^^
</pre></div>
</div>
</div>
</div>
Try this CSS selector:
table:has(th:matches(.+-URLs?)) td:first-of-type pre
DEMO
Related
I'm trying to use Selenium in order to open a HTML page and click a button.
The HTML I get back is:
<html>
<head>
<link rel="shortcut icon" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="http://localhost:5050/style.css">
<title>Test</title>
</head>
<body>
<div class="btn-container">
<button class="btn-orange" id="successButton" name="Success" value="Success"> Success </button>
<button class="btn-orange" id="failButton" name="Fail" value="Fail"></button> Fail
</div>
</div>
<footer class="footer" align="center">
<div class="container-fluid">
<div class="clearfix">
<div class="cards pull-left">
</div>
</div>
</div>
</footer>
</body>
</html>
I'm trying to click the successButton but it does not seem to work, I try to access via its id.
Here is the line I use to click:
driver.findElement(By.id("successButton")).click();
And here's my entire function:
public void openTheHtmlPageAndClickButton(
String pageUrl,
String SiteUrl,
String buttonValue) {
String lastUrl = null;
boolean timeout = true;
for (int tryNumber = 1; tryNumber <= 5 && timeout; tryNumber++) {
WebDriver driver = null;
try {
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), DesiredCapabilities.firefox());
System.out.println("Selenium open: " + pageUrl);
driver.get(pageUrl);
int i = 0;
Alert alert;
while (i++ < 30) {
try {
alert = driver.switchTo().alert();
if (alert != null) {
alert.accept();
}
} catch (NoAlertPresentException e) {
}
String currentUrl = driver.getCurrentUrl();
driver.findElement(By.id("successButton")).click();
if (!currentUrl.equals(lastUrl)) {
System.out.println("currentUrl: " + currentUrl);
lastUrl = currentUrl;
if (currentUrl.startsWith(SiteUrl)) {
timeout = false;
break;
}
} else {
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
Assert.fail();
}
}
}
} catch (Exception e) {
System.out.println("Selenium exception: " + e.toString());
} finally {
if (driver == null) {
Assert.fail("Cannot open web driver, probably Selenium docker is down");
} else {
if (timeout) {
System.out.println("Page got timeout: page source: " + driver.getPageSource());
if (tryNumber == 5) {
Assert.fail("Page got timeout 3 times!!!");
}
}
driver.quit();
}
}
}
}
Please advise on what am I doing wrong.
Apparently the button did not appear when I tried to 'click' it. Changed the timeout slightly and issue was resolved.
Compiled files of chat websocket example that comes with Tomcat 7 works fine on localhost and Openshift.
However, when I compile those files myself it doesn't work on Openshift, although on localhost still works great (tomcat 7).
I tried to compile through netbeans, eclipse, and terminal (OSX). Tried all versions of dependencies, tried building as java webapp, maven webapp, tried to upload as war file but no success.
The module I'm trying to test is the chat websocket. It is composed by two classes, one xhtml file and two dependencies.
working: http://chatoriginal-helioha.rhcloud.com:8000/examples/websocket/chat.xhtml
not working: http://chat-helioha.rhcloud.com:8000/websocket/chat.xhtml
I'm not changing a single line of code, so the files I compile myself should be the same as the ones that comes already compiled right?
I also tried to decompile files using www.javadecompilers.com to see if there was anything extra there but I couldn't find any difference.
There are only three files on this project and they are shown bellow.
ChatAnnotation.java
package websockets.chat;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import util.HTMLFilter;
#ServerEndpoint(value = "/websocket/chat")
public class ChatAnnotation {
private static final Log log = LogFactory.getLog(ChatAnnotation.class);
private static final String GUEST_PREFIX = "Guest";
private static final AtomicInteger connectionIds = new AtomicInteger(0);
private static final Set<ChatAnnotation> connections =
new CopyOnWriteArraySet<>();
private final String nickname;
private Session session;
public ChatAnnotation() {
nickname = GUEST_PREFIX + connectionIds.getAndIncrement();
}
#OnOpen
public void start(Session session) {
this.session = session;
connections.add(this);
String message = String.format("* %s %s", nickname, "has joined.");
broadcast(message);
}
#OnClose
public void end() {
connections.remove(this);
String message = String.format("* %s %s",
nickname, "has disconnected.");
broadcast(message);
}
#OnMessage
public void incoming(String message) {
// Never trust the client
String filteredMessage = String.format("%s: %s",
nickname, HTMLFilter.filter(message.toString()));
broadcast(filteredMessage);
}
#OnError
public void onError(Throwable t) throws Throwable {
log.error("Chat Error: " + t.toString(), t);
}
private static void broadcast(String msg) {
for (ChatAnnotation client : connections) {
try {
synchronized (client) {
client.session.getBasicRemote().sendText(msg);
}
} catch (IOException e) {
log.debug("Chat Error: Failed to send message to client", e);
connections.remove(client);
try {
client.session.close();
} catch (IOException e1) {
// Ignore
}
String message = String.format("* %s %s",
client.nickname, "has been disconnected.");
broadcast(message);
}
}
}
}
HTMLFilter.java
package util;
/**
* HTML filter utility.
*
* #author Craig R. McClanahan
* #author Tim Tye
*/
public final class HTMLFilter {
/**
* Filter the specified message string for characters that are sensitive
* in HTML. This avoids potential attacks caused by including JavaScript
* codes in the request URL that is often reported in error messages.
*
* #param message The message string to be filtered
*/
public static String filter(String message) {
if (message == null)
return (null);
char content[] = new char[message.length()];
message.getChars(0, message.length(), content, 0);
StringBuilder result = new StringBuilder(content.length + 50);
for (int i = 0; i < content.length; i++) {
switch (content[i]) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '&':
result.append("&");
break;
case '"':
result.append(""");
break;
default:
result.append(content[i]);
}
}
return (result.toString());
}
}
chat.xhtml
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Apache Tomcat WebSocket Examples: Chat</title>
<style type="text/css"><![CDATA[
input#chat {
width: 410px
}
#console-container {
width: 400px;
}
#console {
border: 1px solid #CCCCCC;
border-right-color: #999999;
border-bottom-color: #999999;
height: 170px;
overflow-y: scroll;
padding: 5px;
width: 100%;
}
#console p {
padding: 0;
margin: 0;
}
]]></style>
<script type="application/javascript"><![CDATA[
"use strict";
var Chat = {};
Chat.socket = null;
Chat.connect = (function(host) {
if ('WebSocket' in window) {
Chat.socket = new WebSocket(host);
} else if ('MozWebSocket' in window) {
Chat.socket = new MozWebSocket(host);
} else {
Console.log('Error: WebSocket is not supported by this browser.');
return;
}
Chat.socket.onopen = function () {
Console.log('Info: WebSocket connection opened.');
document.getElementById('chat').onkeydown = function(event) {
if (event.keyCode == 13) {
Chat.sendMessage();
}
};
};
Chat.socket.onclose = function () {
document.getElementById('chat').onkeydown = null;
Console.log('Info: WebSocket closed.');
};
Chat.socket.onmessage = function (message) {
Console.log(message.data);
};
});
Chat.initialize = function() {
if (window.location.protocol == 'http:') {
Chat.connect('ws://' + window.location.host + '/examples/websocket/chat');
} else {
Chat.connect('wss://' + window.location.host + '/examples/websocket/chat');
}
};
Chat.sendMessage = (function() {
var message = document.getElementById('chat').value;
if (message != '') {
Chat.socket.send(message);
document.getElementById('chat').value = '';
}
});
var Console = {};
Console.log = (function(message) {
var console = document.getElementById('console');
var p = document.createElement('p');
p.style.wordWrap = 'break-word';
p.innerHTML = message;
console.appendChild(p);
while (console.childNodes.length > 25) {
console.removeChild(console.firstChild);
}
console.scrollTop = console.scrollHeight;
});
Chat.initialize();
document.addEventListener("DOMContentLoaded", function() {
// Remove elements with "noscript" class - <noscript> is not allowed in XHTML
var noscripts = document.getElementsByClassName("noscript");
for (var i = 0; i < noscripts.length; i++) {
noscripts[i].parentNode.removeChild(noscripts[i]);
}
}, false);
]]></script>
</head>
<body>
<div class="noscript"><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websockets rely on Javascript being enabled. Please enable
Javascript and reload this page!</h2></div>
<div>
<p>
<input type="text" placeholder="type and press enter to chat" id="chat" />
</p>
<div id="console-container">
<div id="console"/>
</div>
</div>
</body>
</html>
At general I have website where the main content is the list of posts with text. So I parse every post in this block of HTML code.
<div class="col-xs-12" style="margin:0.5em 0;line-height:1.785em">Some text</div>
For this I created this AsyncTask.
class NewPostsAsyncTask extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Новые");
progressDialog.setMessage("Загрузка...");
progressDialog.setIndeterminate(false);
progressDialog.show();
}
#Override
protected String doInBackground(String... params) {
Document doc;
try {
doc = Jsoup.connect(URL).get();
content = doc.select("[style=margin:0.5em 0;line-height:1.785em]");
titleList.clear();
for (Element contents : content) {
if (!contents.text().contains("18+")) {
titleList.add(contents.text());
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
listView.setAdapter(adapter);
progressDialog.dismiss();
}
}
But I have some problem. All posts are not stored on one web page. You must click to link at the end of all posts for redirecting to another page with posts.
And this block have this HTML code.
<div class="row"><div class="col-xs-12">
<div class="paginator">
<span class="pagina">1683</span> " | "
<span class="pagina">1682</span> " | "
<span class="pagina">1681</span> " | "
<span class="pagina">1680</span> " | "
<span class="pagina">1679</span> " | "
<span class="pagina">3</span> " | "
<span class="pagina">2</span> " | "
<span class="pagina">1</span>
</div>
</div>
</div>
How can I go to another page, parse other posts and print them in ListView after previous posts? As a result I want have all posts of this website in one ListView. Can you show me how I should do it?
Here is how I would do it:
Sample code
#Override
protected String doInBackground(String... params) {
Document doc;
// I supposed URL variable is initialized like this: URL="killpls.me";
try {
do {
doc = Jsoup.connect(URL).get();
content = doc.select("[style=margin:0.5em 0;line-height:1.785em]");
titleList.clear();
for (Element contents : content) {
if (!contents.text().contains("18+")) {
titleList.add(contents.text());
}
}
Element anchor = doc.select( //
"#stories > div:nth-child(3) > div:nth-child(1) > div:nth-child(1) > span.pagina:not(:has(a)) + span > a" //
).first();
if (anchor==null) {
break;
} else {
doc = null;
URL = anchor.absUrl("href");
}
} while(canContinue());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private boolean canContinue() {
// Implement custom logic here ...
// Return true if additionnal posts should be downloaded false otherwise.
return true;
}
Some details
The heart of the method lies on the following line:
Element anchor = doc.select( //
"#stories > div:nth-child(3) > div:nth-child(1) > div:nth-child(1) > span.pagina:not(:has(a)) + span > a" //
).first();
The first() method will return a non null reference as long as a next page exists. When the first page is reached, first() returns null and there is no more page to fetch.
#stories /* Find an element with id `stories`*/
> div:nth-child(3) /* Select its third div child */
> div:nth-child(1) /* Select first div child of the previous div */
> div:nth-child(1) /* Select first div (DIV-a) child of the previous div */
> span.pagina:not(:has(a)) /* Select a span with class `pagina` without any anchor as child */
+ span /* Select closest span next to previous span and child of `DIV-a` */
> a /* Here is the next page to fetch */
I have a HTML document in which I need to update both text and src attribute of IMG tag. I am working in Java. I want to replace following strings in the HTML: DataName, DataText and DataIcon.
<body>
<h1 align="center">DataName</h1>
<div class="tabber">
<div class="tabbertab">
<h2>Info</h2>
<p>DataText</p>
</div>
<div class="tabbertab">
<h2>Pictures</h2>
<div id="album">
<ul class="gallery">
<li>1<img src=DataIcon alt="landscape image 1" title="landscape image 1" /></li>
<li>2<img src="C:\thesis\100GreatP\eclipse_ws\test\data\pictures\1\pyramid2.jpg" alt="landscape image 2" title="landscape image 2" /></li>
</ul>
</div>
</div>
<div class="tabbertab">
<h2>Video</h2>
</div>
</div>
While I have suceeded to replace strings DataName and DataText, I havent succeed to replace DataIcon by my imageURL stored in the database as String. Checking the debug says that it just simply fails to search for DataIcon string. I am using HTMLparser and I have written following class to apply the problem:
public class MyNodeVisitor extends NodeVisitor {
String name;
String text;
String icon;
public MyNodeVisitor() {
}
public MyNodeVisitor(String IconPath, String Name, String Text){
this.name = Name;
this.text = Text;
this.icon = IconPath;
}
public void visitStringNode (Text string)
{
if (string.getText().equals("DataName")) {
string.setText(name);
}
else if(string.getText().equals("DataIcon")){
string.setText(icon);
}
else if (string.getText().equals("DataText")){
string.setText(text);
}
}
}
The class has been applied in my application code in such way
NodeList nl = new NodeList();
String htmlString = null;
InputStream contentStream = null;
String textString = null;
String resultStr = getDatabaseAttribute(name,"DESCRIPTION");
String resultStr2 = getDatabaseAttribute(name,"NAME");
String resultStr3 = getDatabaseAttribute(name,"ICON_path");
try
{
// Read the URL content into a String using the default encoding (UTF-8).
contentStream = WWIO.openFileOrResourceStream(BROWSER_BALLOON, this.getClass());
htmlString = WWIO.readStreamToString(contentStream, null);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
WWIO.closeStream(contentStream, resultStr);
}
try {
Parser parser = new Parser(htmlString);
nl = parser.parse(null);
nl.visitAllNodesWith(new MyNodeVisitor(resultStr3, resultStr2,resultStr));
nl.toString();
} catch (ParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String output = nl.toHtml();
return output;
Can anybody help me? The whole problem is that it fails to search for DataIcon string in IMG tag. Thanks for your help.
Your img tag isn't an StringNode. You need to override the visitTag(Tag tag) method and work on the Tag object.
Something like (not compiled)
public void visitTag(Tag tag) {
if ("img".equals(tag.getTagName())) {
if ("DataIcon".equals(tag.getAttribute("src"))) {
tag.setAttribute("src", icon);
}
}
}
I have a web page that the user pushes a button and initiates an action in a proprietery app (which resides in Tomcat).
That action is a long running process and the only way I have to see what's going on is to log onto the server and look at a log file.
I wrote a quick Java function that reads the log file and gives feedback on what's happening. (Essentially it just tails the file and parses out the things I need)
I'd like to be able to add a jsp that I can view the output without logging into the server.
===
From a design standpoint, I understand that the JSP should return quickly with a result and not just keep on processing.
So my idea is to create a simple web page that queries the jsp for an update and writes the latest info to the screen. Wait 30 seconds, poll the server again, and append the latest update.
What I'm struggling to grasp is how to get the JSP to communicate with the back end process, and how that back end process should be spawned / killed.
This is a very occasional thing (once every two weeks, start to completion takes an hour or two), so I don't want a daemon running all the time. I want to be able to turn it on temporarily and turn it off.
If I spawn a process from within a simple servlet, how do I end that process when I'm done?
And how do I communicate with it?
You can create a java.lan.Runnable witch reads the file content an saves it into a buffer. The Runnable reads the file content withing a while loop for witch the break condition can be set from outside, Thread that is executing your Runnable will terminate whe the run method of your Runnable terminates.
In your JSP you can create a java.lang.Thread and pass an instance of your Runnable to it. Save th instace of the runable in the ServletContext so you can access it across the requests. If you want to terminate the polling than just set the break condition of your Runnable from the JSP, the rum method will terminate and thus the thread too.
You can use javascript setInterval() function and XMLHttpRequest refresh the page.
here is a sample basic implemntation (I hope this will meet your requirements):
Polling Runnable
package com.web;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class FilePollingThread implements Runnable {
private String filepath = null;
private boolean polling = false;
private StringBuffer dataWritenAfterLastPoll = null;
private String error = null;
public FilePollingThread(String filepath) {
this.filepath = filepath;
}
#Override
public void run() {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(filepath)));
dataWritenAfterLastPoll = new StringBuffer();
polling = true;
String line = null;
while(polling) {
try {
line = br.readLine();
while(line == null) {
try {
Thread.sleep(500L);
} catch (InterruptedException e) {
e.printStackTrace();
error = e.toString();
}
line = br.readLine();
}
dataWritenAfterLastPoll.append(markUp(line));
} catch (IOException e) {
e.printStackTrace();
error = e.toString();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
error = e.toString();
} finally {
if(br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
error = e.toString();
}
}
}
}
private String markUp(String line) {
String markup = "";
if(line != null) {
markup = "<div style=\"height: 6px\"><span style=\"line-height: 1.1;\">" + line + "</span></div>\n";
}
return markup;
}
public synchronized void stopPolling() {
polling = false;
}
public synchronized String poll() {
String tmp = markUp(error == null ? "Not ready" : error);
if(dataWritenAfterLastPoll != null) {
tmp = dataWritenAfterLastPoll.toString();
dataWritenAfterLastPoll = new StringBuffer();
}
return tmp;
}
}
And a JSP witch initiats the polling an keep retrieving data
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# page import="com.web.FilePollingThread" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Poll file</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link rel="stylesheet" type="text/css" href="style/default.css"></link>
<script type="text/javascript">
var c = 1;
var ih;
var polling = false;
var filepath = null;
function startPolling(interval) {
ih = setInterval(function () {
try {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
var w = getElementById('ajax_content');
w.innerHTML = w.innerHTML + xmlHttp.responseText;
getElementById('page_refresh').innerHTML = c++;
polling = true;
window.scrollTo(0, document.body.scrollHeight);
} else {
polling = false;
throw 'HTTP ' + xmlHttp.status;
}
}
};
xmlHttp.open('GET', 'pollfile.jsp?filepath=' + filepath + '&c=' + c, true);
xmlHttp.send();
} catch(e) {
alert('Error at startPolling: ' + e);
clearInterval(ih);
}
}, interval);
}
function startStopPolling() {
var orgPolling = polling;
try {
if(polling) {
polling = false;
clearInterval(ih);
doPolling();
} else {
polling = true;
doPolling();
startPolling(1000);
}
flipStartStopButtonsLabel();
} catch(e) {
polling = orgPolling;
flipStartStopButtonsLabel();
alert('Error at startStopPolling: ' + e);
}
}
function flipStartStopButtonsLabel() {
var label;
if(polling) {
c = 1;
label = 'Stop polling';
getElementById('page_refresh').innerHTML = '0';
} else {
label = 'Sart polling';
getElementById('page_refresh').innerHTML = 'stoped';
}
var buttons = document.getElementsByName('start_stop_polling');
if(buttons) {
for(var i = 0; i < buttons.length; i++) {
buttons[i].value = label;
}
}
}
function doPolling() {
var url = 'pollfile.jsp?polling=';
if(polling) {
filepath = getElementById('filepath');
if(filepath && filepath.value && filepath.value.length > 0) {
url += 'true&filepath=' + encodeURIComponent(filepath.value);
} else {
throw 'No filepath specified.';
}
} else {
url += 'false';
}
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status != 200) {
throw 'HTTP ' + xmlHttp.status;
}
}
};
xmlHttp.open('POST', url, false);
xmlHttp.send();
}
function clearWindow() {
var w = getElementById('ajax_content');
if(w) {
w.innerHTML = '';
}
}
function getElementById(id) {
try {
if(id) {
elm = document.getElementById(id);
return elm;
}
} catch(e) {
alert('Error at getElementById: ' + e);
}
return null;
}
</script>
</head>
<body>
<%
String polling = request.getParameter("polling");
if("true".equals(polling)) {
String filepath = request.getParameter("filepath");
if(filepath != null && filepath.length() > 0) {
FilePollingThread pollingThread = new FilePollingThread(filepath);
new Thread(pollingThread, "polling thread for file '" + filepath + "'").start();
request.getServletContext().setAttribute("pollingThread", pollingThread);
}
} else if("false".equals(polling)) {
FilePollingThread pollingThread = (FilePollingThread) request.getServletContext().getAttribute("pollingThread");
if(pollingThread != null) {
pollingThread.stopPolling();
}
} else {
FilePollingThread pollingThread = (FilePollingThread) request.getServletContext().getAttribute("pollingThread");
if(pollingThread != null) {
response.getWriter().println(pollingThread.poll());
response.getWriter().close();
return;
}
}
%>
<div class="label">
<span>Page polling:</span>
</div>
<div style="float: left;">
<span id="page_refresh">0</span>
</div>
<div class="clear_both"> </div>
<form id="input_form" action="pollfile.jsp" method="get">
<div>
<div style="float: left;">
<label>Filepath:
<input style="height: 24px;" id="filepath" type="text" size="120" value=""/>
</label>
</div>
<div style="clear: both;"/>
<div style="float: left;">
<input style="height: 24px;" name="start_stop_polling" id="start_stop_polling_button" type="button" onclick="startStopPolling(); return false;" value="Start polling"/>
</div>
<div style="float: left;">
<input style="height: 24px;" name="clear_window" id="clear_window_button" type="button" onclick="clearWindow(); return false;" value="Clear"/>
</div>
<div style="clear: both;"> </div>
</div>
</form>
<div id="ajax_content">
</div>
<div>
<div style="float: left;">
<input style="height: 24px;" name="start_stop_polling" id="start_stop_polling_button" type="button" onclick="startStopPolling(); return false;" value="Start polling"/>
</div>
<div style="float: left;">
<input style="height: 24px;" name="clear_window" id="clear_window_button" type="button" onclick="clearWindow(); return false;" value="Clear"/>
</div>
<div style="clear: both;"> </div>
</div>
</body>
</html>
EDIT
there is a bug in FilePollingThread: if no data is available in the file the thread might get stuck in inner while loop. it should be
while(line == null && polling)
also the JSP will not work on IE (testet on IE9). It seems that the data writen to the response in the line
response.getWriter().println(pollingThread.poll());
contains the HTML of the hole page. If added to the target div IE seems not able to render it.
I made another version using a simple static HTML file an a servlet as it offers more controle on what is writen to response.
If you are interested in the code let me know.
You should consider using something like JMS to control your background process.
For control, your front-end code can send messages start/stop/inspect the process.
For monitoring, your process can publish to a JMS topic for your front end code to read.