jeudi 21 avril 2022

How can I connect my ember app to servlet runnning on tomcat

I am new to ember,so I have created a servlet to continuously read a log file and have created a component in ember for front end (I have build the emberapp and added it to the webapps in servlet package in tomcat.But I am able to see only html page in my server and when i click enter no response are there in the browser.

filetail.js

import Component from '@glimmer/component';
import {action} from '@ember/object';

export default class FiletailComponent extends Component {
    @action
    tailfile() {
        let filepath = document.getElementById("filepath").value;
        
        $.ajax({
            type: "GET",
      
            url: "/sre",//servlet url
            data: {

                filepath: filepath
            },
            dataType: "text",
            success: function (data) {
                document.getElementById('tail').innerHTML += data;           
            },
            error: function (jqXHR, testStatus, errorThrown) {
                console.log("error thrown: " + testStatus);
            }
            
        });
        
    }
}

filetail.hbs

<form>
    <input type="text" id="filepath" required >
    <label>filepath:</label>
    <input  type="submit" value="Enter">
    <div id="tail"></div>
</form>

sre.java


@WebServlet("/sre")
public class sre extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/plain");  
        PrintWriter printwriter = response.getWriter();
        
        String path=request.getParameter("filepath");
        
        System.out.println("path is "+path);
        File file= new File(path);
        if(!file.exists()) {
            printwriter.println("file doesnot exist");
        }
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
        long length = randomAccessFile.length();
        String temp;
        int foundLine = 0;
        while (length > 0 && foundLine < 10) {
            randomAccessFile.seek(length--);
            if (randomAccessFile.read() == 10) {
                foundLine++;
            }
        }
        while((temp = randomAccessFile.readLine()) != null) 
        {
            printwriter.println(temp);              

        }
        response.flushBuffer();
        
        String parentFolder = file.getParent()+"/";
        Path filepath = Paths.get(parentFolder);
        WatchService watchService = FileSystems.getDefault().newWatchService();
        WatchKey wk = filepath.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
        try 
        {
            while(true){
                wk = watchService.take();
                for (WatchEvent<?> event : wk.pollEvents()) {
                    Path changed = (Path) event.context();
                    if(changed.endsWith(file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("/")+1))){
                        while((temp = randomAccessFile.readLine())!=null) {
                            printwriter.println(temp);              
                        }
                        response.flushBuffer();  
                    }

                }
                boolean valid = wk.reset();
                if (!valid) 
                    break;
            }
        }

        catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}

In short I need to get the file path from the user using emberfor frontend and then pass the filepath to the servlet and then print the contents in file in browser...Any help would be great




Aucun commentaire:

Enregistrer un commentaire