vendredi 18 septembre 2020

How to implement a Web Page to read values stored by a library that uses a Web Filter in Java

I have been working in a requirement that involves the following:

  • Create a library that can be referenced by any project, this library will track some metrics and information such execution time from each request/response. All this information should be stored in memory and not in any other data source such database.
  • Create a Web Page that can display the metrics gathered by the library

So far, what comes to my mind to solve this is to:

Create a simple java project (to work as the library) and create a Web Filter using @WebFilter annotation, something like:

@WebFilter(urlPatterns = {"/*"})
public class MetricGatheringFilter implements Filter {
    @Override
    public void doFilter (ServletRequest request, ServletResponse response,
                          FilterChain chain) throws IOException, ServletException {
     //Gather the information here, using System.nanoTime() and some additional logic for any other information I would like to get 
   }
}

I think that would be enough for the first part of the requirement. I can compile the java project and export is a jar file and then add it as a library to a Web Project I would like to test.

What I am not sure about is how can I create something like a Web UI to take a look of the metrics that are stored in memory.

The simpliest thing that comes to my mind is to store the metrics in something like ServletContext of any Web Application that is using the library and then create a JSP file that could reference those variables that are stored and display them to anyone that would like to see that information.

On the other hand, something I think that could work is to implement some logic inside the filter to store the information in some kind of temporary files. Then, I can create something like "Metrics Web UI Reporter" which is a Java Web application that can lookup for the temporary file and read the metrics to display them in an "user friendly" way.

Just in case, I am not asking for the whole code to be answered in this post, I only would like to know if there is another way to implement this kind of requirement.

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire