'Calling a servlet programmatically when a specific criteria is met

I am working on a JSP app and I have successfully created an HttpServlet that allows user to download a file on the browser. I have tested it by calling the servlet using and href tag and the file is downloaded as it should.

Now, I want instead of using href to call the servlet when some criteria is met. For example I have the following JAVA code:

if (criteria.isMet())  {
   
   String linkToCallServlet = "the servlet link";
   HttpURLConnection conn = (HttpURLConnection) new URL(linkToCallServlet).openConnection();
   conn.setRequestMethod("GET");
   conn.getInputStream();

}

When I execute this code the servlet is executed as it should but the file is not downloaded on browser's window.

Also, no error appears. Any idea why? I have tested on Chrome and Edge



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source