In my project, 1) I have added logger using logback. The logger writes in a file while running a project.
2) I want to read this file while it is being written by logger and display output of it on html/jsp page.
3) To read file I want to send an ajax request from html/jsp page and display output as line by line. i.e. read one line and display it on html page.
Here is what I did-----
//To read file
try {
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
out.println(line);
}
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// ex.printStackTrace();
}
And my html page is
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"> </script>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function funReadFile(){
//alert("ok");
$.ajax({
type:"GET",
// contentType: "text/plain; charset=utf-8",
url: '/manageCollector/readFile',
success : function (response){
$("#myResponse3out").html(response);
},
error: function(jqXHR, textStatus, errorThrown) {
alert('error ' + textStatus + " " + errorThrown);
}
});
}
</script>
</head>
<body onload = "funReadFile()">
<textarea name="textarea" id="myResponse3out" rows="27" cols="70" ></textarea>
<h2>Test</h2>
</body>
</html>
How do I Read file line by line and display it??