How to read from a file line by line using an ajax request, and the file is written by another program using java?

In my project, 1) I added a logger using logback. The registrar writes a file during project execution.

2) I want to read this file while it is being written by the registrar and displays it on the html / jsp page.

3) To read the file, I want to send an ajax request from the html / jsp page and display the output as line by line. i.e. read one line and display it on the 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

<!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 to read a file line by line and display it?

0
source share
1 answer

JQuery .

$("#animate").click(function() {
    $("#content")
        .animate({"height": "100px"}, {"queue": false, "duration": 500})
        .animate({"width": "250px"}, 500);
}); 

Jquery Log Viewer, .

0

Source: https://habr.com/ru/post/1648049/


All Articles