How get all links from a web page and its sub pages in Java
<p>I wrote a program to get all the links from a webpage and then get all the links from that sub page.<br></p>

<p><strong>For example:</strong> I have page <a href="http://www.example.com" rel="nofollow">www.example.com</a> and found five other links: <a href="http://example.com/home" rel="nofollow">example.com/home</a>, <a href="http://example.com/contact" rel="nofollow">example.com/contact</a>, etc. I should go to these links and check again for links on that webpage.</p>

<p><strong>How should I implement this?</strong></p>

<p>Also, I need a list of already visited pages and blocked pages (Link to Facebook, for example, because then I think it will be in the infinite loop).</p>

<p>Here is my code so far (Which only gives me the links from one webpage and not its subpages):</p>

<pre><code> try {
        Document doc = Jsoup.connect("https://www.example.com/").get();
        Elements elements = doc.select("a");
        for(Element element : elements){
            System.out.println(element.absUrl("href"));
            list.add(element.absUrl("href"));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
</code></pre>