5
I have a Java web application with Applets that access services on the web. These services are accessed through a specific hostname (example: services.webapp.com.br
) configured in the application. Knowing that it is the user’s JVM that will execute the code referring to the Applets' lifecycle, it is up to this JVM to translate the host services.webapp.com.br
at an IP address that will be effectively used by services.
However, it may happen that the user edit your Windows\System32\drivers\etc\hosts
and redirect the host services.webapp.com.br
to another IP that is not the address of my services, so the application will not be able to access them and an error will happen.
I know that beyond the hosts
Windows there are other ways to redirect traffic to a different IP, however, I want to cover this point because I know that my users use it.
In other words, I need to know at runtime if there is any input for the host services.webapp.com.br
in the archive hosts
of the user’s machine that is using my applet, to display an alarm if it is true. Is there an elegant way to do this implementation?
In a few hours of searching on Google, I saw that the only solution would be to directly read the contents of this file using the Java I/O libraries as in this example, because it is something of low level of the operating system and it escapes from the scope of Java.
Sorry for not posting code, because my biggest doubt is in the concept of implementation.
(...) vi que a única solução seria ler diretamente o conteúdo desse arquivo utilizando as bibliotecas de I/O (...)
That’s right. Any other solution you find probably does just that underneath the covers.(...) é algo de baixo nível do sistema operacional e foge do escopo do Java (...)
. Try implementing this in C or Assembly before calling the implementation in low-level Java ;) and no, it does not escape the scope of Java at all. The framework and language were made for cases like this too.– Oralista de Sistemas
Probably I end up implementing on my own, because outside the portability between the systems based on Mac, Windows and Linux, I don’t see big challenges to the point of having a big change in the development time if I find a framework. I didn’t call the implementation in low-level Java, what I meant was basically that, taking into account the portability of Java and maintenance cost, I don’t know if I should expect Java to have libraries (own or third party) to change a specific file, of a specific operating system.
– Bruno Gasparotto