Convert Java console program to Webapp

I have a Java class with main() . It contains logic to perform a number of crunch and analysis operations. It is planned to be launched once a day, and if necessary, it can be started manually. The procedure uses Log4j to record its actions. Starting and checking the log requires remote login to the host.

I would like to convert it to a web application where I can run it through a web page and see the log output, ideally when it scrolls.

What would be the best way to do this? Java webapp? Simple generic web resource script? or any other option that I don’t know about.

Update: A little more details about the requirements:

  • The ability to run the program from a web page
  • Ability to see scroll output from Log4j
  • If I leave the page and go back, she should inform me that the last run is still running and shows the log. I do not need to be able to run multiple instances in parallel.
+6
source share
3 answers

Have you considered setting up a Hudson / Jenkins server to complete a task? Since it has the functions that you describe, it is a java web application and it will work unchanged in your existing project.

+5
source

You can submit the form via AJAX and use Ajax with Spring MVC to poll new log entries and add them to the table on your page. Here's the quick code for calling AJAX to check for a new log entry using JQuery and Spring's MVC controller method to handle it.

JSP:

 $.getJSON("logs.htm", { lastLogId: logId }, function(response) { $('#myTable tr:last').after('<tr><td>' + response + '</td></tr>'); }); 

Spring MVC controller (returns JSON):

 @Controller public class LogController { @RequestMapping("logs.htm") public @ResponseBody String getLogs(@RequestAttribute("lastLogId") Integer lastLogId, HttpSession sess) { LogList logs = sess.getAttribute("logs"); // just an example using user-defined class "LogList" return logs.getNextLog(lastLogId); } } 

There are several parts that I did not mention here (it would be necessary to write log entries to the session when log4j logs, etc.), but I hope that at least it is useful to see a way to do this using Spring MVC and AJAX.

+1
source

you can try calling the web service from jsp / jsf

0
source

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


All Articles